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!

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.