Static Using Statements (C# 6)

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 about this feature. In my honest opinion (IMHO), this feature is not meant to be used everywhere in your code. Like salt, a little enhances the flavor—but too much can cause issues.

With that warning out of the way, let’s see what static using does. There are times when the class name breaks the flow of code, lowering readability. Here’s some example code we’ll work with:

using System;

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

The repeated use of the [Math](https://msdn.microsoft.com/en-us/library/system.math%28v=vs.110%29.aspx) class makes this a bit harder to read. We can use the new static using statement feature to remove that redundancy:

using System;
using static System.Math;

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

Notice that while we’ve removed Math. from line 8, we’ve added a new static using statement on line 2. This line tells the compiler where to look if it can’t find the method or property in the current scope. Again, this is just syntactic sugar—the resulting code is functionally identical to the first example.

The only requirement is that the class be static. This means it works with Math, Console, Convert, and any of your own static classes as well.


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

If there’s already a member in scope with the same name—for example, if we define a Cos method—this is what happens:

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

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

In this scenario, the locally defined member (Cos) takes precedence, as it’s the first one the compiler finds. This is an example of where overusing this feature could lead to subtle, hard-to-debug naming conflicts.


What about Extension Methods?

C# 3.0 introduced extension methods, which let you write methods that appear to be part of a class but are actually separate. Here’s an example where we add Example() to the string class:

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 a static using statement still lets you call extension methods as usual, but you cannot call them directly without qualifying them. For direct calls, you must include the class (and possibly the namespace):

using System;
using static ExtensionMethods.Extensions;

internal class StaticUsing
{
    public static void BrokenFlow()
    {
        Console.WriteLine(ExtensionMethods.Extensions.Example("Leslie")); // Works
        Console.WriteLine(Example("Robert")); // Does NOT work
        Console.WriteLine("Robert".Example()); // Works
    }
}