Thomas Ardal

Entrepreneur and founder of elmah.io

Declaration Expressions and Static Using in C# 6.0

One feature considered and ultimately not added to C# 6 was declaration expressions.

In the previous post, inspired by The Future of C# talk at GOTO, I looked at Primary Constructors. This post is about two nice little improvements in C# 6.0 called Declaration Expressions and Static Using.

As usual let’s start by looking at a piece of code:

bool result;
if (bool.TryParse("true", out result))
{
    Console.WriteLine("This is a bool with value. " + result);
}
else
{ 
    Console.WriteLine("Not a bool, yo! " + result);
}

To check if the string value is a bool, we use the bool.TryParse method, which returns true if the string can be parsed as a bool. The parsed value is set on the out parameter named result.

Having to declare the result bool outside the scope of the if-else always bugged me. With Declaration Expressions the future suddenly seems brighter:

if (bool.TryParse("true", out var result))
{
    Console.WriteLine("This is a bool with value. " + result);
}
else
{ 
    Console.WriteLine("Not a bool, yo! " + result);
}

It may be hard to spot other differences than the missing declaration of result, but take a closer look just after the out keyword. A new var keyword crept in between out and result, declaring the result bool in the scope of the if-else only. Please also notice, that the result variable is now only accessible inside the if-part, but also the else-part.

Not surprisingly the decompiled code looks similar to the first example:

bool flag;
if (bool.TryParse("true", out flag))
{
    Console.WriteLine("This is a bool with value. " + flag);
}
else
{
    Console.WriteLine("Not a bool, yo! " + flag);
}

Before we wrap up, let’s spend just a second looking at another new feature of C# 6.0: Static Using. I began my career coding Java, but shifted towards .NET back in 2006. A couple of years ago, I had to write some Java code (don’t ask) and one of the new features which had been introduced in the Java language since I left it where Static Imports. After my quick Java endeavors, I actually missed Static Imports in .NET, why I’m happy that we now get Static Using. To boil down, static using let you import static methods from other types directly into your current class. Let’s look at how we can improve the bool example to utilize static using:

using System.Console;
 
...
 
if (bool.TryParse("true", out var result))
{
    WriteLine("This is a bool with value. " + result);
}
else
{ 
    WriteLine("Not a bool, yo! " + result);
}

We start by declaring a new using statement. Unlike your normal using, the static using actually resolves to a type name and not a namespace, in this case the Console type. When utilized, all of the static members of Console are available to our current type, making it possible to execute WriteLine without having the prepend it with Console. The used type needs to be declared static itself, which means that you can’t declare a using of a non-static class, even it contains static methods. This makes it ideal for utilities like Console and Math.

Show Comments