Introduction
On November 12, 2014, the day of the Visual Studio Connect() event, Microsoft announced Visual Studio 2015 Preview with many new and exciting features for developers for testing purposes. Microsoft announced the new version of C#, C# 6.0 that came with many improvements and new features. One of the newly introduced features of C# 6.0 is Using Static Class Statements.
Don't forget to read my previous posts on this series: "A new feature of C# 6.0"
What does Static class statements mean.?
Using static is a new kind of clause that allows us to import static members of types directly into scope. We can include a static class in the using statement similar to a namespace. As we know a static class cannot be instantiated. When using a static member, to access any static member we need to repeat the class name. For example, Console is a static class and ReadLine(), WriteLine(), ReadKey(), and Clear() are the methods of the Console class. The following code snippet describes the usage of various members of the Console class.
As we all can see we are repeating the Console class again and again to get access to the members. C# 6.0 allows us to avoid repeating the class name again and again by simply adding using the static class. The following code snippet shows that.
We started by declaring a new using statement "using Sytem. Console;". The using static actually resolves to a type name. It is when utilized, that all of the static members of the Console class are available to our current type that makes it possible to execute the WriteLine() and ReadLine() methods without adding Console at the beginning.
The second code looks much cleaner without the use of the Console class many times. The outputs of both the code snippets are the same.
![Democode snippet]()
This was just a simple example of using the System. Console class as a namespace, but in the .NET Framework there are multiple static classes that can be included in the using statement similar to a namespace.
Demo Application using Visual Studio 2013
Output
![VS13 output]()
Demo Application using Visual Studio 2015 Preview
Output
![Output]()
Summary
In this article, we learned how to use multiple static classes that can be included in the statement similar to a namespace that makes our source code more maintained, cleaner, and readable. Don't forget to read my other articles in the series "A new feature of C# 6.0". Share your opinion about this feature and how will you use it in your project. Your comments are most welcome. Happy Coding!