Using Static Statements Improvements in C# 6.0

Introduction

This article explains the use of static members. As we know Microsoft announced many new features with Visual Studio 2015 Preview. It was announced on November 12, 2014 on the day of the Visual Studio Connect() event in New York, USA.

Visual C# 6.0, .NET Framework 4.6 and the Roslyn Compiler are now available with Visual Studio 2015 Preview.

First we need to understand how static members are used. Static is a keyword basically in C# that makes methods and variables special in the sense that there is no need to create an object for these static members. However these members are accessed by their class reference that is very helpful in the coding style and also memory management is good enough with static members. This produces optimized code as well.

The same concept is used to overcome the headaches of programmers; it introduces the elimination of the use of a class when using static members, mainly methods. It gets a bit redundant and it would certainly be great if we could include a class's static members as a part of our own type, making it easier to access the methods. Consider the usage of Debug.WriteLine() or Console.WriteLine() for instance. A namespace like using System.Console must be written at the top to use all the members of Console class.  Previously we needed to use a class reference when accessing the static methods.

For example previously Console.WriteLine() was used to write in C# 5.0 but now it is converted to WriteLine() in C# 6.0.

However all these talks could easily be understood with the following code.

program.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.console;  
  7.   
  8. namespace ConsoleApplication8  
  9. {  
  10.     public class Weeks  
  11.     {  
  12.         public void days()  
  13.         {  
  14.             WriteLine("enter the values between 1 to 7");  
  15.             int x = Int32.Parse(ReadLine());  
  16.   
  17.             if (x == 1)  
  18.                 WriteLine("the day is monday");  
  19.             else if (x == 2)  
  20.                 WriteLine("the day is tuesday");  
  21.             else if (x == 3)  
  22.                 WriteLine("the day is wednesday");  
  23.             else if (x == 4)  
  24.                 WriteLine("the day is thrusday");  
  25.             else if (x == 5)  
  26.                 WriteLine("the day is friday");  
  27.             else if (x == 6)  
  28.                 WriteLine("the day is saturday");  
  29.             else WriteLine("the day is sunday");  
  30.   
  31.         }  
  32.     }  
  33.     class Program  
  34.     {  
  35.         static void Main(string[] args)  
  36.         {  
  37.   
  38.             // making object of ""weeks" class  
  39.   
  40.             Weeks obj = new Weeks();  
  41.             obj.days();  
  42.              
  43.             ReadKey();  
  44.         }  
  45.   
  46.     }  
  47. }  
Although the preceding code is good enough, when executed in the .NET Framework 4.5 or Visual Studio 2013 Ultimate then the errors produced are like this as shown below.


here we can see that all the methods reporting errors when run in 2013 ultimate

errors in c# 6.0

With all the preceding code we saw, we came to understand that we can't write these methods alone without using their class reference but now we will implement the same code in the enhanced version of C# 6.0 and Visual Studio 2015 preview. Let us see what happens next.

Program.cs*
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.console;  
  7. // this code is running on 2015 preview  
  8. namespace ConsoleApplication8  
  9. {  
  10.     public class Weeks  
  11.     {  
  12.         public void days()  
  13.         {  
  14.             WriteLine("enter the values between 1 to 7");  
  15.             int x = Int32.Parse(ReadLine());  
  16.   
  17.             if (x == 1)  
  18.                 WriteLine("the day is monday");  
  19.             else if (x == 2)  
  20.                 WriteLine("the day is tuesday");  
  21.             else if (x == 3)  
  22.                 WriteLine("the day is wednesday");  
  23.             else if (x == 4)  
  24.                 WriteLine("the day is thrusday");  
  25.             else if (x == 5)  
  26.                 WriteLine("the day is friday");  
  27.             else if (x == 6)  
  28.                 WriteLine("the day is saturday");  
  29.             else WriteLine("the day is sunday");  
  30.   
  31.         }  
  32.     }  
  33.     class Program  
  34.     {  
  35.         static void Main(string[] args)  
  36.         {  
  37.   
  38.             // making object of ""weeks" class  
  39.   
  40.             Weeks obj = new Weeks();  
  41.             obj.days();  
  42.              
  43.             ReadKey();  
  44.         }  
  45.   
  46.     }  
  47. }  
Output

weeks output2015 preview

Using the System.Math namespace

The same case as above is explained with the math class however this can be understood using the code given below. However this code is written using Visual Studio 2013 Ultimate and has the .NET framework 4.5 such that this produces the output the same as produced by Visual Studio 2015 Preview but the variation is just the namespace inclusions and using the builtin Math class.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7.   
  8. namespace ConsoleApplication8  
  9. {  
  10.     // this is program on C# 5.0 with Visual Studio 2013 ultimate  
  11.   public  class Circle  
  12.     {   
  13.         int radius = 5;  
  14.   public void Area()  
  15.   {  
  16.       // here we are using Math class to call PI and Power function  
  17.   
  18.     double x = Math.PI * Math.Pow(radius, 2);  
  19.     Console.WriteLine("Area of circle is =" +x);  
  20.   }  
  21. }  
  22.   
  23.   
  24. class test   
  25. {  
  26.     public static void Main(String[] args)  
  27.     {  
  28.         Circle cr = new Circle();  
  29.         cr.Area();  
  30.         Console.ReadKey();  
  31.     }  
  32. }  
  33.   
  34. }  
Output

  area circle

Now the following code is different but the output is the same, there are few changes like including the namespaces System.Console and System.Math so there is no need to write Math before builtin functions in the Math package.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Math;  
  7. using System.Console;  
  8.   
  9.   
  10. namespace ConsoleApplication8  
  11. {  
  12.     // this is program on C# 6.0 with Visual Studio 2015 preview  
  13.     public class Circle  
  14.     {  
  15.         int radius = 5;  
  16.         public void Area()  
  17.         {  
  18.             // here we are not using Math class to call PI and Power function  
  19.             //we included namespace using System.Math; and console also  
  20.   
  21.             double x = PI * Pow(radius, 2);  
  22.             WriteLine("Area of circle is =" + x);  
  23.         }  
  24.     }  
  25.     class test  
  26.     {  
  27.         public static void Main(String[] args)  
  28.         {  
  29.             Circle cr = new Circle();  
  30.             cr.Area();  
  31.             ReadKey();  
  32.         }  
  33.     }  
  34.   
  35. }  
Note: check this code carefully, we have not written Math before the builtin functionalities even though we are using it.

Output

output 2015
  
Summary

In this article we learned new features of C# 6.0 that was introduced in Visual Studio 2015 Preview. Since all these features reduce some overhead of the programmer to write the same name many times. These two namespaces are included in the code and this reduces the compiler's effort as well as the programmers effort and produces optimized code. 

Up Next
    Ebook Download
    View all
    Learn
    View all