Posts Tagged C# 6.0

What’s new in C# 6.0

C# has seen several changes since it got launched when .NET Framework came out. e.g. C# 2 had closures (anonymous methods), generics, C# 3 introduced LINQ, C# 5 saw task-based asynchronous pattern and now we have C# 6.0. Although you can enable C# 6.0 in Visual Studio 2013 but it’s not recommended since not all the new features will be available in the older IDE. If you still want to persist with 2013 then the plugin can be downloaded from https://github.com/dotnet/roslyn. The best way to use C# 6.0 is to get Visual Studio 2015. Currently, RC is available for download but the final version isn’t far away.

Here are some of the newer features in C# 6.0

1. Number Literal Formats

How many times did we have to write some number and then count the digits to see if it’s thousand, hundred thousand or million. E.g. in the past we declared numbers like this:

Thousand Comma Underscore

var thousand = 1000;

var tenThousand = 10000;

var hundredThousand – 100000;

var million = 1000000;

I despise counting digits to ensure it’s the right number. C# 6.0 to the rescue now we can declare them like this:

var thousand = 1_000;

var tenThousand = 10_000;

var hundredThousand – 100_000;

var million = 1_000_000;

As you can see an underscore (_) is doing the trick here.

Binary Literals

Binary numbers are easier to write. So, we can use something like this:

int binary_1 = 0b00_00_00_00_00_00_01; // 0x0001
int binary_2 = 0b00_00_00_00_00_00_10; // 0x0001
That will make things a lot easier when it comes to using bit shifting operators. e.g.
i
nt one = binary_2 >> 1; 

2. Declaration Expression

This essentially allows you to declare variables in certain scenarios. E.g. let’s examine int.TryParse
var convertedValue;
var isConverted = int.TrParse(“123”, out converted);
Here, we had to declare “converted” variable upfront before using it in int.TryParse. 
In C# 6.0 now it’s possible to do the same thing in a single line like this:
var isConverted = int.TryParse(“123”, out var converted);
As you can see we were able to use var, or we can use explicit type, along with out keyword.

3. Static Using Statements

We have several static class / functions in C#. e.g. Console, File etc. 
Let’s see how code was written before C# 6.
   1:  using System;
   2:   
   3:  public class Program
   4:  {
   5:      public static void Main()
   6:      {
   7:          Console.WriteLine("Please enter your name");
   8:          var name = Console.ReadLine();
   9:          Console.WriteLine("Welcome, " + name);
  10:      }
  11:  }

In C# 6.0 it’s possible to use static using statements so we don’t have to use static class name over and over again. So, the new code will look like this:

   1:  using System;
   2:  using System.Console; // this line is doing the magic
   3:   
   4:  public class Program
   5:  {  
   6:         public static void Main()
   7:        {
   8:                WriteLine("Please enter your name"); // no need to write Console
   9:                var name = ReadLine(); // no need to write Console
  10:                WiteLine("Welcome, " + name); // no need to write Console
  11:         }
  12:  }

4. Auto Properties with Initializers

auto properties were available in the past. So, we were able to do some thing like this:

   1:  class Country
   2:  {
   3:    public int CountryId {get; set;}
   4:    public int CountryName {get; set;}
   5:  }
   6:   

Auto properties used to have problem when it used to be an array or collection. So, for a collection we had to create a private backing field or we had to use a constructor. With backing field it used to look like this:

   1:  class Country
   2:  {
   3:    public int CountryId {get; set;}
   4:    public int CountryName {get; set;}
   5:    public List<string> Provinces {get; set;}
   6:   
   7:    public Country()
   8:    {
   9:       Provines = new List<string>(); // initializing collection in constructor
  10:    }
  11:  }
  12:   

Now, in C# 6.0 it’s possible to use auto property syntax and initialize it as well in a single line. New code looks like this:

   1:  class Country
   2:  {
   3:    public int CountryId {get; set;}
   4:    public int CountryName {get; set;}
   5:    public List<string> Provinces {get; set;} = new List<string> ;
   6:  }
   7:   

There are several more new features in C# 6.0, like indexed members, element initializer and primary constructors.

,

No Comments