Concise C# Part 3 : C# 3 shortcuts

The version 3 of C# brings us a lot of syntactic sugar to reduce the length of our code. Here are some of them.

This post is part of a serie about making C# code shorter.

Constructors with Property initializers

//Verbose and Ugly
Post value = new Post();
value.Title = "New Version For C#";
value.CreatedOn = DateTime.Now;
CreatePost(value);

//Concise and Sweet
CreatePost(new Post
{
    Title = "NewVersion For C#",
    CreatedOn = DateTime.Now
});

Collection initializers

//Verbose and Ugly
var names = new List();
names.Add("manitra");
names.Add("yeah");
SaveNames(names);

//Concise and Sweet
SaveNames(new List { "manitra", "yeah" });

Var keyword

//Verbose and Ugly
Dictionary> result = new Dictionary>();

//Concise and Sweet (But still strongly typed)
var result = new Dictionary>();

Automatic properties

//Verbose and Ugly
public class Post
{
    private string title;
    private string description;
    private DateTime createdOn;

    public string Title
    {
        get
        {
            return this.title;
        }
        set
        {
            this.title = value;
        }
    }

    public string Description
    {
        get
        {
            return this.description;
        }
        set
        {
            this.description = value;
        }
    }

    public System.DateTime CreatedOn
    {
        get
        {
            return this.createdOn;
        }
        set
        {
            this.createdOn = value;
        }
    }
}

//Concise and Sweet
public class Post
{
    public string Title { get; set; }
    public string Description { get; set; }
    public DateTime CreatedOn { get; set; }
}

Concise C# Part 2 : Try Catch and reusability

Exception handling is an important part of any software. In this post, I will talk about a trick to make but some kind of reusability on exception handling.

This post is part of a serie about making C# code shorter

When you write code, you usually try to reuse your code by creating methods and calling those methods each time you the need their functionality. When working with exceptions, it’s a bit tricky because, they don’t follow the normal code flow, they jump out of your method … unless you catch them. The consequence is that you usually have multiple similar try {} catch {} blocks that are often copied and pasted.

This is what usually happens

public void Method1()
{
    try
    {
        //method 1 content
    }
    catch (ThreadAbortException)
    {
    }
    catch (IOException ex)
    {
        //IO exception handling
    }
    finally
    {
        //add any code for releasing ressource
    }
}

public void Method2()
{
    try
    {
        //method 2 content
    }
    catch (ThreadAbortException)
    {
    }
    catch (IOException ex)
    {
        //IO exception handling
    }
    finally
    {
        //add any code for releasing ressource
    }
}

The solution to reuse the exception block

This is what you could do to reuse the same error handling code block in c# :

public void Method1()
{
    Do(() =>
    {
        //method 1 content
    });
}

public void Method2()
{
    Do(() =>
    {
        //method 2 content
    });
}

protected void Do(Action method)
{
    try
    {
        //code to call before each methods
        method();
        //code to call after each methods success
    }
    catch (ThreadAbortException)
    {
    }
    catch (IOException ex)
    {
        //IO exception handling
    }
    finally
    {
        //add any code for releasing ressource
    }
}