Using Static Class Statements: A New Feature of C# 6.0

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.

  1. static void Main(string[] args)  
  2.         {  
  3.             Console.Clear();  
  4.             Console.Write("\n Enter your name: ");  
  5.             string name = Console.ReadLine();  
  6.             Console.WriteLine("\n Name: {0}", name);  
  7.             Console.ReadKey();  
  8.         }  

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.

  1. using Sytem.Console;  
  2. static void Main(string[] args)  
  3. {  
  4.    Clear();  
  5.    Write("\n Enter your name: ");  
  6.    string name = ReadLine();  
  7.    WriteLine("\n Name: \{name}");  
  8.    ReadKey();  
  9. }  

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.

democode snippet

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

  1. using System;  
  2. using System.Collections.Generic;  
  3. namespace CSharpFeatures  
  4. {  
  5.     class usingsystemVS13  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             Console.WriteLine("\n -- Use of System.Math Class --\n");  
  10.             int a = 10, b = 3, remainder, radius = 2;  
  11.             int answer = Math.DivRem(a, b, out remainder);    // using System.Math Class method DivRem    
  12.             Console.WriteLine(" Result of 10/3 : {0}", answer);  //using System.Console class method WriteLine  
  13.             Console.WriteLine(" Remainder of 10/3 : {0}", remainder);  
  14.             double _tan = Math.Tan(0);   // using System.Math Class method Tan  
  15.             Console.WriteLine(" Value of Tan(0) : {0}", _tan);  
  16.             double circlearea = Math.PI * Math.Pow(radius, 2);    // using System.Math Class methods PI and Pow  
  17.             Console.WriteLine(" Area of Circle when radius is {0} : {1}", radius, circlearea);  
  18.             Console.WriteLine("\n -- Use of System.Convert Class --\n");  
  19.             string number = "5";  
  20.             int convertednum = Convert.ToInt32(number);   // using System.Convert Class method ToInt32  
  21.             Console.WriteLine(" Number converted from string to int : {0}", convertednum);  
  22.             Console.ReadKey();  //using System.Console class method ReadKey  
  23.         }  
  24.     }  
  25. }  

Output

VS13 output

Demo Application using Visual Studio 2015 Preview

  1. using System;  
  2. using System.Console;  
  3. using System.Convert;  
  4. using System.Math;  
  5. namespace CSharpFeatures  
  6. {  
  7.     class usingsystemVS15  
  8.     {  
  9.         static void Main(string[] args)  
  10.         {  
  11.             WriteLine("\n -- Use of System.Math Class --\n");  
  12.             int a = 10, b = 3, remainder, radius = 2;  
  13.             int answer = DivRem(a, b, out remainder);  //using System.Math class method DivRem  
  14.             WriteLine(" Result of 10/3 : \{answer}");  //using System.Console class method WriteLine  
  15.             WriteLine(" Remainder of 10/3 : \{remainder}");  
  16.             double _tan = Tan(0);  //using System.Math class method Tan  
  17.             WriteLine(" Value of Tan(0) : \{_tan}");  
  18.             double circlearea = PI * Pow(radius, 2);  //using System.Math class method PI and Pow  
  19.             WriteLine(" Area of Circle when radius is \{radius} : \{circlearea}");  
  20.             WriteLine("\n -- Use of System.Convert Class --\n");  
  21.             string number = "5";  
  22.             int convertednum = ToInt32(number);  //using System.Convert class method ToInt32  
  23.             WriteLine(" Number converted from string to int : \{convertednum}");  
  24.             ReadKey();  //using System.Console class method ReadKey  
  25.         }  
  26.     }  
  27. }  

Output

VS15 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!