Roslyn – C# Compiler

I’m obviously very glad that the compiler has now been rewritten from scratch allowing
it to support many new features. Some I consider very handy and some I just don’t get (not in the sense that I don’t know what they do, but rather what benefit they bring to the .NET world) As a C# developer I will focus on C# features.

Null-Conditional Operator

One of the improvements, they say is the conditional check for null:
Whereas before you had to write something like this to check for null

public static string Truncate(string value, int length)
{
    string result = value;
    if (value != null) // Skip empty string check for elucidation
    {
        result = value.Substring(0, Math.Min(value.Length, length));
    }
    return result;
}


Now it's much shorter:

public static string Truncate(string value, int length)
{
    return value?.Substring(0, Math.Min(value.Length, length));
}

At first it seems like a very handy tool, but my worry is that this feature will be greatly abused. Some developers would be tempted to write this type of code:

public static string Truncate(string value, int length)
{
    return value?.Item1?.Item1?.Customer?.FirstName;
}

Obviously the alternative would be much bulkier, however this approach takes away from the developer the ability to throw a 'NullReferenceException'. Say we always expect the 'Customer' to be not null. If it's null it's a bug, so an exception has to be thrown. With the shorthand notation it's very easy to miss that.

However in this case it's quite handy although only a couple of lines of code are eliminated

OnTemperatureChanged?.Invoke(this, value)

Auto-Property Initialisers

I have mixed fillings about this. This generally allows you to create a auto-property with a 'get' accessor only while still being to supply a value:

public class FingerPrint
{
    public DateTime TimeStamp { get; } = DateTime.UtcNow;
}

"As the code shows, property initializers allow for assigning the property an initial value as part of the property declaration. The property can be read-only (only a getter) or read/write (both setter and getter). When it’s read-only, the underlying backing field is automatically declared with the read-only modifier. This ensures that it’s immutable following initialization."

When I first saw this I was unsure whether it would return current of 'DateTime.UtcNow' or the static value.

Nameof Expressions

This one I think is a very useful feature and gives you an ability to get the name of the parameter.

public void Foo(string message)
{
    if(string.IsNullOrWriteSpace(message))
    {
       throw new ArgumentException(nameof(message));
    }
}

This would also come in handy when implementing 'INotifyPropertyChanged' interface. Currently you either have to use expressions or '[CallerNameAttribute]'

Primary Constructors

This alloys you to write constructors in the following manner:

public struct Pair(T first, T second)
{
    public T First { get; } = first;
    public T Second { get; } = second;
}

The whole purpose is to eliminate code bloat and I think this is quite intuitive.

Expression Bodied Functions and Properties

public override string ToString() = string.Format("{0}, {1}", First, Second);

I can see its use. However some developers will be inclined to implement a more complex logic using this technique. So the rule should be that this is used for cases when the override is very simple. If you have to use

public override string ToString()
{
    //complex logic
}

In this case it makes more sense to do an override using the old fashioned way.

I'm glad that Microsoft decided not to go ahead with index properties, this was just another way of doing something that you could already do.

Using Static Methods

I like this feature. Quite often you have to work with a static class, for example Math and then you end up with Math.Min(..) * Math.Pow(..) etc. The new feature would eliminate having Math. , by importing the class like you would import a namespace:

using System.Math;
public static void Main(string [] args)
{
    var result = Min(x, 0.5) * Max(y, 1);
}

References:
http://msdn.microsoft.com/en-us/magazine/dn802602.aspx
https://roslyn.codeplex.com/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: