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"
- Exception Filtering: A New Feature of C# 6.0
- String Interpolation: A New Feature of C# 6.0
- Name of Operator: A New Feature of C# 6.0
- Auto Property Initializer: A New Feature of C# 6.0
- Dictionary Initializers: A New Feature of C# 6.0
- Using Await in Catch and Finally Blocks: A New Feature of C# 6.0
- Expression Bodied Members: A New Feature of C# 6.0
- Null Propagation Operator: 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.
static void Main(string[] args)
{
Console.Clear();
Console.Write("\n Enter your name: ");
string name = Console.ReadLine();
Console.WriteLine("\n Name: {0}", name);
Console.ReadKey();
}
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.
using System.Console;
static void Main(string[] args)
{
Clear();
Write("\n Enter your name: ");
string name = ReadLine();
WriteLine($"\n Name: {name}");
ReadKey();
}
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.




K P Singh ChundawatPosted Jan 10, 2015, 11:34 AM
nice..