C# : 6 Cool Language Features That You Rarely Use

These features were included as a part of C# in its 6th installment – C# 6.0. Relatively easy to master, they bring a new way of achieving the same programming goals. It can be a little hard to keep up with changing language syntax. But then, that’s the field we chose !

The features I will discuss, are not drastic language changes, but are still a welcome change. These are minor syntactical changes that make coding a little more simpler. Let’s get going.

1. Automatic Property Initialization :

C# had already got rid of the need to declare your private variables before exposing them in properties, which was a few versions ago. In C# 6.0, we go one step ahead see the introduction of auto property initialization that can be done at the time of initialization.

public class MyClass
{
    public string Id { get; } = "assignment";
}

2. Dictionary Initializers

If we look at the way key/value pair is initialized, we see that it is more intuitive and make more sense writing it this way.

Dictionary<string, MyClass> _value =
            new Dictionary<string, MyClass>()
            {
                ["key1"] = new MyClass("value1");
                ["key2"] = new MyClass("value2");
            }


3. Declaration expressions :

You can declare the local expressions in the body of the loop. There isn’t a need to declare the expressions before the loop begins.
See below snippet where we have the variable – “onlyOnes” declared in the foreach declaration.

int result = 0;
foreach(var n in var onlyOnes = numbers.Where(n => n == 1).ToList())
{
    //do something and populate the result variable.
}
return result;

4. Use the ‘await’ keyword inside the catch block :

With the async and await keywords, we can pause the execution of the program till the code prefixed with the await keyword is not executed. However, the UI thread is not blocked, and this creates a more responsive UI.

In C# 6.0, the await keyword can now be used inside catch blocks like so,

catch (Exception ex)
{
     await _worker.DoProcess("failed");
}

 5. Declaring Static class with other namespaces :

Simply declare the Static class with the other namespaces instead of appending before the method call each time.

using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace NewNamespace
{
    class Program
    {
        static void Main(string[] args)
        {
            WriteLine("Hi there !");
        }
    }
}

The Console.WriteLine() can be simply invoked as WriteLine() now.

6. Null-Conditional Operator :

Consider the below snippet :

public static string MyFunc(string value1, int value2)
{
    return value1?.Substring(0, 2);
}

Assume that the variable – ‘value1’ is null. This will result in NullReference exception when we try to call Substring(). As you can see, we have conditional operator ( ? ) after ‘value1’, which has been introduced in C# 6.0. Basically, this checks whether value1 is null. if it is, then there wouldn’t be any call to Substring() and we can avoid the exception.

 

4 thoughts on “C# : 6 Cool Language Features That You Rarely Use

Leave a Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.