Easy to Access Static Methods in C# 6.0

Content

As we all know Microsoft has launched a new version of C# called C# 6.0 with Visual Studio Ultimate 2015 Preview and there is new feature in C# 6.0 that is based on "Static Classes".

This feature allows us to access the static methods without using Class name each and every time. Let's see this feature.

I am using Visual Studio Ultimate 2015 Preview.

Visual Studio Ultimate 2015

Open Visual Studio 2015 and select "File" -> "New" -> "Project..." and fill in the project name as "Console Application1".

Project

After creating the project, I will access some common static methods in "program.cs" without the help of class name every time.

Basically we used to access the static methods using class name like:

  • Console
  • Convert
  • Math

Console: Generally we use to access the methods of the "Console" Class until C# 5.0.

  1. class Program  
  2. {  
  3.    static void Main(string[] args)  
  4.    {  
  5.       Console.WriteLine("Hi");  
  6.    }  
  7. }  
Note: Here I am printing "Hi" using the "WriteLine()" method of the "Console" class.

But in C# 6.0, we can access the methods of the "Console" Class or static class directly. For this we need to access the "Console" class using "using" like namespaces.
  1. using System.Console;  
  2.   
  3. class Program  
  4. {  
  5.    static void Main(string[] args)  
  6.    {  
  7.       WriteLine("Hi");  
  8.    }  
  9. }  
You can see there is a successful build with this code in Visual Studio Ultimate 2015 Preview.

code

Convert: Similarly we previously accessed the methods of the "Convert" Class until C# 5.0.
  1. class Program  
  2. {  
  3.    static void Main(string[] args)  
  4.    {  
  5.       int i = Convert.ToInt32(8.0);  
  6.    }  
  7. }  
Note: Here I am converting the "double" value into an "Int" that is 8.0 using the "ToInt()" method of the "Convert" class.

But in C# 6.0, we can access the methods of the "Convert" class or static class directly. For this we need to access the "Convert" class using a "using" like namespaces.
  1. using System.Convert;  
  2.   
  3. class Program  
  4. {  
  5.    static void Main(string[] args)  
  6.    {  
  7.       int i = ToInt32(8.0);  
  8.    }  
  9. }  
You can see there is successful build with this code in Visual Studio Ultimate 2015 Preview.

cs code

Math: Similarly we previously accessed the methods of the "Math" Class until C# 5.0.
  1. class Program  
  2. {  
  3.    static void Main(string[] args)  
  4.    {  
  5.       Double i = Math.Sqrt(8.0);  
  6.    }  
  7. }  
Note: Here I am doing a Square Root of the "double" value that is 8.0 using the "Sqrt()" method of the "Math" Class.

But in C# 6.0, we can access the methods of the "Math" class or static class directly. For this we need to access the "Math" class using "using" like namespaces.
  1. using System.Math;  
  2.   
  3. class Program  
  4. {  
  5.    static void Main(string[] args)  
  6.    {  
  7.       Double i = Sqrt(8.0);  
  8.    }  
  9. }  
You can see there is a successful build with this code in Visual Studio Ultimate 2015 Preview.

build code

Conclusion: Now you understand the new feature of C# 6.0 that allows the use of the "Static Methods" in an easy way.

 


Similar Articles