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

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.

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.

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

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);   // using System.Math Class method DivRem
            Console.WriteLine(" Result of 10/3 : {0}", answer);  // using System.Console class method WriteLine
            Console.WriteLine(" Remainder of 10/3 : {0}", remainder);
            double _tan = Math.Tan(0);   // using System.Math Class method Tan
            Console.WriteLine(" Value of Tan(0) : {0}", _tan);
            double circlearea = Math.PI * Math.Pow(radius, 2);    // using System.Math Class methods PI and Pow
            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);  // using System.Convert Class method ToInt32
            Console.WriteLine(" Number converted from string to int : {0}", convertednum);
            Console.ReadKey();  // using System.Console class method ReadKey
        }
    }
}

Output

VS13 output

Demo Application using Visual Studio 2015 Preview

using System;
using static System.Console;
using static System.Convert;
using static 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); //using System.Math class method DivRem
            WriteLine(" Result of 10/3 : {answer}"); //using System.Console class method WriteLine
            WriteLine(" Remainder of 10/3 : {remainder}");
            double _tan = Tan(0); //using System.Math class method Tan
            WriteLine(" Value of Tan(0) : {_tan}");
            double circlearea = PI * Pow(radius, 2); //using System.Math class method PI and Pow
            WriteLine(" Area of Circle when radius is {radius} : {circlearea}");
            WriteLine("\n -- Use of System.Convert Class --\n");
            string number = "5";
            int convertednum = ToInt32(number); //using System.Convert class method ToInt32
            WriteLine(" Number converted from string to int : {convertednum}");
            ReadKey(); //using System.Console class method ReadKey
        }
    }
}

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!


Similar Articles