dev csharp

It’s a shorthand for writing methods. The body now can be written just like a Lambda expression as shown in Log2 method below:

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

public string Log2(string timestamp, string application, string error, string status) => string.Format("[Timestamp: \{timestamp}], Application: [\{application}], Error: [\{error}], Status [\{status}]");

It may come in handy for helper methods. The only benefit I can see is getting rid of opening and closing curly braces which generally don’t bother me much. But I know lots of people trying to avoid curly braces as much as possible. I’m sure this feature will be popular among them.

dev csharp

Currently using statements are for namespaces only. With this new feature they can used for static classes as well. Like this:

using System.IO;
using System.IO.File;

namespace CSharp6Features
{
    class UsingStaticClass
    {
        public class StaticUsing
        {
            public StaticUsing()
            {
                File.WriteAllText("C:\test.txt", "test");
                WriteAllText("C:\test.txt", "test");
            }
        }
    }
}

I don’t think I liked this new feature. If you see a direct method call it feels like it’s a member of that method. But now it’s possible that method can be defined inside a static class somewhere else. I think it would just cause confusion and doesn’t add any benefit.

dev csharp

Currently, in Visual Studio 2013, if you have a line like this

public int MyProperty { get;  }

you’d get a compilation error like this:

Getter-only auto-property error

But the same code in VS 2015 compiles happily. The reason to add this feature is to not get in the way of immutable data types.

Another new feature about auto-properties is initializers. For example the following code would compile and run with new C#:

public class AutoInit
{
    public string FirstName { get; } = "Unknown";
    public string LastName { get; } = "Unknown";

    public AutoInit()
    {
        Console.WriteLine(string.Format("{0} {1}", FirstName, LastName));
        FirstName = "Volkan";
        LastName = "Paksoy";
        Console.WriteLine(string.Format("{0} {1}", FirstName, LastName));
    }
}		

and the output is unsurprisingly looks like this:

Auto-property initializer output

When I first ran this code successfully I was surprised how I managed to set values without a setter. Looks like under the covers it’s generating a read-only backing field for the property and just assignning the value to the field instead of calling the setter method. It can easily be seen using a decompiler:

Just Decompile output

As it’s a read-only value it can only be set inside the constructor. So if you add the following method it wouldn’t compile:

public void SetValue()
{
    FirstName = "another name";
}

Auto-property set error

It’s a small improvement providing an alternative way to write the same code in less lines.