Saturday, September 20, 2014

What's New in C# 6

1) Auto property initializer:

With C# 6 initialize auto properties just like a field at declaration place. The only thing to notice here is that this initialization dose not cause the setter function to be invoked internally. the value of backing field is set directly. so here is an example:

public class User
    {
//Auto property initializer
            public Guid Id {get;}=Guid.NewGuid();
    }

2) Primary Constructor:

Primary Constructor allow us to places the parameters for a constructor immediately after the type name and use those parameters inside initializer expression in the rest of the type definition. This save us to write explicit constructor with a lot of  right to left assignment.

public class struct Money(string currency,decimal amount)
    { 
            public string Currency {get;} = currency;
            public decimal Amount {get;} = amount;
    }

3) using Static:

This feature allows you to specify a particular type in a using statement after that all static members of that type will be accessible is subsequent code.

using System.Console
 class Program
    { 
            public static void Main()
            {
                   WriteLine("Hello World");
           }
    }

4) Dictionary Initializer:

some people believed that the old way of initiailzing dictionaries was dirty so the C# team dicided to make it cleaner, thanks to them. here is an example of the old way and new way:

// the old way of initializing a dictionary
            Dictionary oldWay = new Dictionary()
            {
                { "Afghanistan", "Kabul" },
                { "United States", "Washington" },
                { "Some Country", "Some Capital city" }
            };

            // new way of initializing a dictionary
            Dictionary newWay = new Dictionary()
            {
                // Look at this!
                ["Afghanistan"] = "Kabul",
                ["Iran"] = "Tehran",
                ["India"] = "Delhi"
            };


5) ?. Operator

This operator allows to DE-reference valid pointer but it will give me  a null back instead of a null reference exception if object is null.

var name = operation ?. Method ?. Name ?? "No name"

6) Exception filters

Exception filters are already supported in VB compiler but now they are coming into C#. exception filters lets you specify a condition for a catch block. the catch block gets executed only if the condition is satisfied, this one is my favorite feature, so let's look at an example:


           try
            {
                throw new Exception("Me");
            }
            catch (Exception ex) if (ex.Message == "You")
            {
                // this one will not execute.
            }
            catch (Exception ex) if (ex.Message == "Me")
            {
                // this one will execute
            }

7) Declaration expressions:

This feature simply allows you to declare local variable in the middle of an expression. It is as simple as that but really destroys a pain. I have been doing a lot of asp.net web form projects in the past and this was my every day code:
int result = 0 ;
 foreach(var n in var odd= numbers.Where(n=> n%2 ==1).ToList())
{
 result+= n + odd.Count();
}
return result;

No comments:

Post a Comment