01 Apr 2015
C# 6
Want to learn about other C# 6 features? Check out the full list of articles & source code on GitHub

Static using statements are a new feature of C# 6 designed to improve the readability of code. Before we begin: a word of warning for this feature. IMHO This feature is not meant to be used everywhere in your code. This feature is like salt, a little is great – too much, can cause issues.

With the warning out of the way, lets see what it does? There are times where the class name can break the flow of the code, lowering readability. Here is some example code we will work with:

using System;

internal class StaticUsing
{
    private void BrokenFlow()
    {
        Console.WriteLine(Math.Cos(5) * Math.Tan(20) + Math.PI);
    }
}

The addition of the Math class makes this a little harder to read, so we can use the new static using statement feature to remove that:

using System;
using static System.Math;

internal class StaticUsing
{
    private void BrokenFlow()
    {
        Console.WriteLine(Cos(5) * Tan(20) + PI);
    }
}

Note, that while we have removed the Math class from line 8, we have added a new using statement with the static keyword in line 2. This line tells the compiler where to look if it cannot find the method/property in the current scope. This is just syntactic sugar again and the resulting code is the same as in the first example.

The only requirement for this to work is that the class is that the class is static. That means this works with Math, Console, Convert & any of YOUR own static classes too.

What happens if I have a member with the same name already in scope?

What happens if there is already a member in scope with the same name, for example here I have defined a Cos method:

public static void BrokenFlow()
{
    Console.WriteLine(Cos(5) * Tan(20) + PI);
}

public static double Cos(int value)
{
    return -1;
}

In this scenario it uses the locally defined member first, as that is the first one which the compiler finds. This is an example of where you can shoot yourself in the foot by using this feature too often and running into hard to find conflicts.

What about Extension Methods?

C# 3.0 added support for extension methods which let you write methods which appear to be part of the underlying class but are actually separate. I’ve added the example method to the string class in the following example:

namespace ConsoleApplication1
{
    using System;
    using ExtensionMethods;

    internal class StaticUsing
    {
        public static void BrokenFlow()
        {
            Console.WriteLine("Robert".Example());
        }
    }
}

namespace ExtensionMethods
{
    public static class Extensions
    {
        public static string Example(this string s)
        {
            return "Hello " + s;
        }
    }
}

Adding the static using statement still allows extension methods to be used as normal, however you cannot call the extension method directly this way. If you want to call the extension method directly, you must add the class (& potentially the namespace) in front of the method as you do with C# 5 and before.

using System;
using static ExtensionMethods.Extensions;

internal class StaticUsing
{
    public static void BrokenFlow()
    {
        Console.WriteLine(ExtensionMethods.Extensions.Example("Leslie")); // this works
        Console.WriteLine(Example("Robert")); // this does NOT work
        Console.WriteLine("Robert".Example()); // this works
    }
}
Tags: 

Add new comment