Getting Started With C# 6.0

I have just installed Visual Studio 2015 and was playing with C# 6.0.

It has loads of new features supported out of the box that are mainly for making the code clean and lean.

Some of the features supported by C# 6.0 are already listed in the following link: What is new in C# 6.0.

I tried to run a sample application that uses some of the new features and found it very interesting.

Null checks using the conditional operator (? :) (unofficially called the Elvis operator) and String.Format using \{} (also called string interpolation) and loads of such things are supported by the Framework out of the box.

Here is a sample code that can get you started:

  1. using System.Console;  
  2. namespace VS2015Sample  
  3. {  
  4.     class Program  
  5.     {  
  6.         static void Main(string[] args)  
  7.         {  
  8.            string version = ".Net 4.6";  
  9.            object vsEditor = null;  
  10.            WriteLine("Sample executing with VS 2015 on \{version} \{vsEditor?.ToString()}");  
  11.            vsEditor = "VS 2015";  
  12.            WriteLine("Sample executing with VS 2015 on \{version} \{vsEditor?.ToString()}");  
  13.             ReadLine();  
  14.         }  
  15.     }  
  16. }  
cs code

If you see in the code above, I am not using Console.WriteLine or Console.ReadLine anywhere. I am also not having null checks for displaying the vsEditor details. Also, String.Format is not used anywhere for formatting display strings.

The output of the code above will be as depicted below:

depicted

C# 6.0 looks promising and will help the developer fraternity with many simple tricks.

Happy coding! 


Recommended Free Ebook
Similar Articles