C# 6


Thew C# 6 features we will create a new Console application in Visual Studio. New -> New Console project. Right click on the project in Solution Explorer and then set the language options Properties -> Build -> Advanced -> Language version -> C#6
  •  using static keyword. Using this feature means that you can do away with fully qualified namespace resolution. Instead of Console.WriteLine() you can simply do this
using static System.Console;  
... 
WriteLine("welcome to blog");  
  • Auto property initializer. The names says it all, but in case you're unsure, you can initialize properties with default values, without the need for a constructor, like this
public class Customer  
{
    public int CusId { get; set; }
    public string CusName { get; set; } = "Hillary Page";
    public int CusAge { get; set; }
}
  • Dictionary Initializers. There's a new way for initializing dictionaries:
static Dictionary<string, string> GetTeachers()  
{
    // old way
    var teacherDictionary = new Dictionary<string, string>
    {
        {"Mr martin", "Height" },
        {"Win Greek", "Job" },
        {"Mrs Lela", "Life" }
    };

    // new way
    var teacherDictionary2 = new Dictionary<string, string>
    {
        ["Mr martin"] = "Height",
        ["Jade Smith"] = "Job" ,
        ["Mrs Lela"] = "Life"
    };
    return teacherDictionary; 
}
  • Exception Filters: No more unwinding of the stack!
public static void TestException()  
{
    try
    {
        throw new Exception("Error");
    }
    catch (Exception ex) when (ex.Message == "ReallyBadError")
    {
        // this one will not execute.
    }
    catch (Exception ex) when (ex.Message == "Error")
    {
        // this one will execute
        WriteLine("This one will execute");
    }
}
  • Async/Await in try/catch/finally
public static async void TestExceptionAsync()  
{
    try
    {
        throw new Exception("Error");
    }
    catch
    {
        await Task.Delay(2000);
        WriteLine("Waiting 2 seconds");
    }
    finally
    {
        await Task.Delay(2000);
        WriteLine("Waiting 2 seconds");
    }
}
  • nameof. This function can be used instead of magic strings when needing to reference variable names in strings
public static void TestNameOf(string name)  
{
    WriteLine(name);
    if(string.IsNullOrEmpty(name))
    {
        WriteLine($"Empty parameter provided: {nameof(name)}");
    }
}
  • String Interpolation. This feature allows you to use any variable within a string composition without needing to use "concatenation" or string.Format(). You'll notice that we also get full IntelliSense.
static void Main(string[] args)  
{
    var customer = new Customer { CustId = 1, CusAge = 30 };
    WriteLine($"The customer name is: {customer .CusName}");
    }
}
  • Null Conditional Operator. The operator checks to see if a referenced property is initialised (not-null) and then returns the value. If the property under evaluation is not initialised, it will return null.
var someValue = person?.Name;
Another cool feature with the Null Conditional Operator is that you can daisy chain calls like so:
string[] names = person?.Name?.Split(' ')
In this case, Split() will only be invoked if both person and person.Name are not null :)

Video Reference