February 18, 2015

Another new feature of C# 6.0 is Exception Filters which allows catching all exceptions except for some. We can basically write an if statement before entering the catch block meaning that we can and cannot enter the catch block. For example, we can catch all exceptions except for say ArithMatic exception or we can check the value of a property before entering the catch block.

Here is an example of how this would work.

private void GetData()
{
    try
    {
        //do something
    }
    catch(Exception ex)
    if(ex.GetType() != typeof(ArithmeticException) && ex.GetType() != typeof(SqlException) && ex.InnerException != null)
    {
        //handle exception
    }
    finally
    {
        //do something
    }
}

Read on other new features of C# 6.0.

One of the new features introduced in C# 6.0 is Conditional Access Operator. This really means that we can use the ? and ?? operators while checking if a property of an object exists. Previously, we had to write if(object != null) and so on but now there is a shorthand of writing this using the conditional operators.

Here's an example of how it works.

private string GetName(NewStuff stuff)
{
    string ans = "";
    //old way
    if (stuff != null && stuff.Name != null)
        ans = stuff.Name.ToUpper();
    else
        ans = "no name";


    //new way
    ans = stuff?.Name? .ToUpper() ?? "no name";

    return ans;
}

Read on other new features of C# 6.0.

February 17, 2015

C# 6.0 has introduced a new feature that allows specifying Static Classes in the using statement. For example, previously we would have written System.Console.WriteLine("some message") or Console.WriteLine("some message") but now we can just write WriteLine("some message") and specify the class name in the using statement. Note that the syntax has recently changed from using StaticClass to using static StaticClass. Here's an example of how to use the static class.

using System;
using static System.Console;
using static System.Math;

 

public static void LogMessage(string message)
{
    WriteLine(message);
}

public static void GetArea(int radius)
{
    //the old way
    Console.WriteLine(Math.PI * Math.Pow(radius, 2));

    //the new way
    WriteLine(PI * Pow(radius, 2));
}

 

As part of the example, just note how much less typing we would need while using static.

Read on other new features of C# 6.0.

This is a new feature in C# 6.0. There is no syntax improvement but a feature improvement and this is new feature allows await keyword to be called within the catch section of a try-catch block. This allows for async exception handling when async results in an exception.

Here is an example of how to use the code.

  1. //await within a catch block
  2. async void DoSomething()
  3. {
  4.     try
  5.     {
  6.         NewStuff stuff = new NewStuff();
  7.         Task<bool> task = stuff.BuggyMethod();
  8.     }
  9.     catch (Exception ex)
  10.     {
  11.         await Logger.Log(ex.StackTrace);
  12.     }
  13.  
  14. }

Read on other new features of C# 6.0.

With the Auto Property initialization feature, properties can be initialized without using private setter or a local variable. Previously, we used to set the properties within the constructors, with C# 6, the properties can be initialised within the properties' get-set section. A constructor is not required.

Here is an example of how this can be achieved.

Property Initializer
  1. class NewStuff
  2. {
  3.     public long ID { get; } = -1;
  4.     public string Name { get; } = "John";
  5.     public string Email { get; protected set; } = "example@domain.com";
  6. }
  7.  
  8. class OldStuff
  9. {
  10.     private string email = "example@domain.com";
  11.     public OldStuff()
  12.     {
  13.         //initialiser
  14.         ID = -1;
  15.         Name = "John";
  16.     }
  17.  
  18.     public long ID { get; set; }
  19.  
  20.     public string Name { get; set; }
  21.  
  22.     public string Email
  23.     {
  24.         get { return email; }
  25.         protected set
  26.         {
  27.             email = value;
  28.         }
  29.     }
  30. }

 

Read on other new features of C# 6.0.

Reference: Shahed Kazi at AspNetify.com