dev csharp

This is another handy feature. Checking for null values before accessing them can quickly become cumbersome and yields a lot of boilerplate code. With this new operator checking for nulls and coalescing becomes really short and easy to read.

For example:

public class NullConditionalOperators
{
    public void Run()
    {
        Person person = GetPerson();

        // Current C#
        if (person != null && person.Country != null)
        {
            Console.WriteLine(person.Country.Name);
        }
        else
        {
            Console.WriteLine("Undefined");
        }
    }

    private Person GetPerson()
    {
        return new Person() { Firstname = "Volkan", Lastname = "Paksoy" };
    }
}

public class Person
{
    public string Firstname { get; set; } = "Unknown";
    public string Lastname { get; set; } = "Unknown";
    public Country Country { get; set; }
    
}

public class Country
{
    public string Name { get; set; }
    public string IsoCode { get; set; }
}

In the example above if you need to print the name of the country first you need to ensure both the Person and Country objects are not null. The if block aobe can be reduced to a one-liner with 6.0:

	Console.WriteLine(person?.Country?.Name ?? "Undefined");

They both produce the same results. The more complex the object hierarchy becomes the more useful this feature would be.

dev csharp

In current C# a collection initialization can be done like this:

var result = new Dictionary<string, string>();
result.Add("index1", "value1");
result.Add("index2", "value2");

or key - value pairs can be added during initialization

var result = new Dictionary<string, string>() 
{
	{"index1", "value1"},
	{"index2", "value2"}
};

With C# 6.0 values at specific indices can be initialized like this:

var result = new Dictionary<string, string>() 
{
	["index1"] = "value1",
	["index2"] = "value2"
};

It’s a shorthand but not so much! I don’t see much value in this notation but I’m sure in time it will prove itself. I don’t think the guys in the language team are just adding random features!

dev csharp

One of favorite features is the new string formatting using String Interpolation. In the past I encountered a lot of errors while formatting strings especially when preparing log messages. You may need lots of small pieces of data that so after a few iterations you may forget to add new parameters.

For example in the imaginary Log method below only 3 parameters are supplied whereas the string expects 4. It compiles successfully because the string is generated at run-time and it doesn’t check the number curly braces against the number of parameters supplied.

Argument count mismatch error

Using the new feature such errors can be avoided as we can put the values directly in their places in the string:

public class StringInterpolation
{
    public string Log(string timestamp, string application, string error, string status)
    {
        return string.Format("[Timestamp: \{timestamp}], Application: [\{application}], Error: [\{error}], Status [\{status}]");
    }
}

No more parameter mismatch errors!