Introduction
On November 12, 2014, the day of 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 Using Static Class Statements means
Using static is a new kind of using 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 with the static class. The following code snippet shows that.
- using Sytem.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, all of the static members of 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.
This was just a simple example 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
- using System;
- using System.Collections.Generic;
- namespace CSharpFeatures
- {
- class usingsystemVS13
- {
- static void Main(string[] args)
- {
- Console.WriteLine("\n -- Use of System.Math Class --\n");
- int a = 10, b = 3, remainder, radius = 2;
- int answer = Math.DivRem(a, b, out remainder);
- Console.WriteLine(" Result of 10/3 : {0}", answer);
- Console.WriteLine(" Remainder of 10/3 : {0}", remainder);
- double _tan = Math.Tan(0);
- Console.WriteLine(" Value of Tan(0) : {0}", _tan);
- double circlearea = Math.PI * Math.Pow(radius, 2);
- Console.WriteLine(" Area of Circle when radius is {0} : {1}", radius, circlearea);
- Console.WriteLine("\n -- Use of System.Convert Class --\n");
- string number = "5";
- int convertednum = Convert.ToInt32(number);
- Console.WriteLine(" Number converted from string to int : {0}", convertednum);
- Console.ReadKey();
- }
- }
- }
Output
Demo Application using Visual Studio 2015 Preview
- using System;
- using System.Console;
- using System.Convert;
- using System.Math;
- namespace CSharpFeatures
- {
- class usingsystemVS15
- {
- static void Main(string[] args)
- {
- WriteLine("\n -- Use of System.Math Class --\n");
- int a = 10, b = 3, remainder, radius = 2;
- int answer = DivRem(a, b, out remainder);
- WriteLine(" Result of 10/3 : \{answer}");
- WriteLine(" Remainder of 10/3 : \{remainder}");
- double _tan = Tan(0);
- WriteLine(" Value of Tan(0) : \{_tan}");
- double circlearea = PI * Pow(radius, 2);
- WriteLine(" Area of Circle when radius is \{radius} : \{circlearea}");
- WriteLine("\n -- Use of System.Convert Class --\n");
- string number = "5";
- int convertednum = ToInt32(number);
- WriteLine(" Number converted from string to int : \{convertednum}");
- ReadKey();
- }
- }
- }
Output
Summary
In this article we learned how to use multiple static classes that can be included in the using statement similar to a namespace that make 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!