Version 6 of C# contains a bunch of new features that allows to speed up the productivity of the developer.

Feature 1 : static usings

The classes are structured by their usings. If you need a specific class, you’ll need to add a using containing the namespace of that class. If you do this, you’ll get access to all the classes that are defined in that using. With static usings, you can bring only a specific class into “scope” which will you access to all of it’s static methods.

Consider following console application.

using System;

namespace ConsoleApplication8
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello world");
        }
    }
}

If i use a static using for the “Console” class, i can do this.

using static System.Console;

namespace ConsoleApplication8
{
    class Program
    {
        static void Main(string[] args)
        {
            WriteLine("Hello world");
        }
    }
}

Feature 2 : ? a.k.a the elvis operator

Consider following object graph.

    public class ObjectA
    {
        public ObjectB ObjectB { get; set; }
    }

    public class ObjectB
    {
        public ObjectC ObjectC { get; set; }
    }

    public class ObjectC
    {
        public string Name { get; set; }
    }

If you want to access the “Name” property of “ObjectC” via ObjectA, i’ll write code that looks like this.

    public void SomeMethod(ObjectA objectA)
    {
        string name = null;

        if (null != objectA.ObjectB)
            if (null != objectA.ObjectB)
                if (null != objectA.ObjectB.ObjectC)
                    name = objectA.ObjectB.ObjectC.Name;
    }

I’ll needs those if-statements because you don’t know if the child object isn’t null. With the elvis operator ? you can remove all the if-statements and make the code a little more readable.

    public void SomeMethod(ObjectA objectA)
    {
        string name = objectA?.ObjectB?.ObjectC?.Name;
    }

If one of the child objects is null, we’ll get the default value for a “string” object.

Feature 3: Expression Bodied Members

Expression Bodies Members give you the possibility to write short methods even shorter.

    public class ObjectC
    {
        public string Name { get; set; }

        public override string ToString()
        {
            return Name;
        }
    }

The “ToString” method can be writing shorter using expressions.

    public class ObjectC
    {
        public string Name { get; set; }

        public override string ToString() => Name;
    }

You can only do this for expression on a single line. If you would write it like this, it won’t build and Visual Studio will give build errors.

    public class ObjectC
    {
        public string Name { get; set; }

        public override string ToString() =>  
        {
            return Name;
        }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>