The “using static” statement

This is one of a series of posts on some of the new features in C# 6.0, which was just released. I had to be careful about naming this one because just calling it “using static” implies a different topic!

The using static is a new type of “using” statement that you can put at the top of your file declaration, but instead of referencing a namespace, you reference a specific class or enum:

using static System.Console;

This allows you to reference the static members of the class directly without including the class name:

public void SomeMethod()
{
  WriteLine("Twas Brilig");
  ReadLine("");
}

instead of

public void SomeMethod()
{
  Console.WriteLine("Twas Brillig and the slithy toves...");
  Console.ReadLine("");
}

Although, of course, you may have conflicts if you include multiple classes.

I think that this is one of those things that can be useful if used judiciously, but has the opportunity for confusion if overused.

 

Here are links to posts about other new features in C# 6.0:

Leave a Reply

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

*