C# 6 Preview: using static

Update 31st January 2015: The syntax for using static has changed as from VS2015 CTP5. Please see C# 6 Preview: Changes in VS2015 CTP 5 for the latest syntax and examples. This article remains available due to historical significance.

Take a look at this little VB .NET program:

Module Module1

    Sub Main()

        Console.WriteLine("Hello world!")
        Console.ReadLine()

    End Sub

End Module

In VB .NET, you can take the Console static class name out of there and put it in the imports:

Imports System.Console

Module Module1

    Sub Main()

        WriteLine("Hello world!")
        ReadLine()

    End Sub

End Module

In fact, that feature works for any static class. And now, this feature is being added to C# starting from version 6. Let’s see the same example in C#. This is as you would write it in C# 5:

using System;

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

And this is how it looks like with the new static using feature in C# 6:

using System.Console;

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

Although most examples I’ve seen use this kind of scenario, I don’t think it’s a very good example of where this feature is useful. In fact, by extracting the static class name from the code that uses it, we’re reducing readability, and we’re also introducing the risk of ambiguity with other static classes that define the same method names. In this case we can’t add a static using for System.Diagnostics.Trace (which also has a WriteLine() method), but it’s quite easy to write a static Logger class that also defines a WriteLine() method, and that can be problematic.

However, the static using feature can be very useful in code that uses static methods heavily. A good example is mathematical calculations. Consider this code:

using System;

namespace UsingStaticMath
{
    class Program
    {
        static double CalculateSomething(double angle)
        {
            var numerator = Math.Sin(angle) * Math.Sin(angle) + Math.Cos(angle);
            var denominator = 1 + Math.Sqrt(Math.Sin(angle) + Math.Pow(Math.Cos(angle), 3));

            return Math.Pow(numerator / denominator, 2);
        }

        static void Main(string[] args)
        {
            var result = CalculateSomething(Math.PI / 2);

            Console.WriteLine(result);
            Console.ReadLine();
        }
    }
}

We can make it a whole lot more concise by introducing the using static:

using System;
using System.Math;

namespace UsingStaticMath
{
    class Program
    {
        static double CalculateSomething(double angle)
        {
            var numerator = Sin(angle) * Sin(angle) + Cos(angle);
            var denominator = 1 + Sqrt(Sin(angle) + Pow(Cos(angle), 3));

            return Pow(numerator / denominator, 2);
        }

        static void Main(string[] args)
        {
            var result = CalculateSomething(PI / 2);

            Console.WriteLine(result);
            Console.ReadLine();
        }
    }
}

In this case, when you read the code, it’s pretty obvious that Sin(), Cos(), Pow() and Sqrt() are mathematical functions, and it’s also very unlikely that there will be an ambiguity.

So in summary, the using static feature can be useful in some scenarios and disruptive in others. Use it wisely.

Leave a Reply

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