What is new in C# 6.0

October 17, 2014 by Anuraj

.Net ASP.Net Visual Studio

Today I got a chance to look into C# 6.0 features. Thanks to ORTIA(Technology innovation division of Orion India Systems), for providing me the MSDN subscription. :)

  1. Exception filters : Exception filters lets you specify a condition for a catch block. The catch block gets executed only if the condition is satisfied.
try
{
    throw new CustomException(10);
}
catch (CustomException ex) if (ex.ErrorCode == 10)
{
    Console.WriteLine("Error code is 10");
}
catch (CustomException ex) if (ex.ErrorCode == 20)
{
    Console.WriteLine("Error code is 20");
}

The above code will write 10, as the Error code property is set to 10.

  1. Declaration expressions - This feature simply allows you to declare local variable in the middle of an expression.
string Id = "10";
int result = 0;
if(int.TryParse(Id, out result))
{
    Console.WriteLine(result);
}

The above code snippet can be re-written as like the following in C# 6.0

string Id = "10";
if (int.TryParse(Id, out int result))
{
    Console.WriteLine(result);
}
  1. using static - This feature allows you to specify a particular type in a using statement after that all static members of that type will be accessible is subsequent code.
using System.Console;

namespace HelloWorldCS6
{
    class Program
    {
        static void Main(string[] args)
        {
            WriteLine("Hello C# 6.0");
        }
    }
}
  1. Auto property initializer - With C# 6 initialize auto properties just like a field at declaration place.
class HelloWorld
{
    //Initiailizing auto property.
    public string Name { get; set; } = "dotnetthoughts";
    //Readonly autoproperty initialization.
    public bool IsExists { get; } = true;
}
  1. Primary Constructor - This feature helps to capture parameters of primary constructor to fields, in the class for furthur processing. When declaring a primary constructor all other constructors must call the primary constructor using :this().
class HelloWorld(string name)
{
    public string Name { get; set; } = name;
}
  1. Dictionary Initializer - C# made the Dictionary more cleaner.
var dictionary = new Dictionary<int, string>()
{
    { 10, "A" },
    { 20, "B" }
};

Can be re-written like this

var dictionary = new Dictionary<int, string>()
{
    [10] = "A",
    [20] = "B"
};

Note : These are experimental features, you have to explicitly enable this, otherwise VS2014 will show an error, while compiling like this. - Feature ‘primary constructor’ is only available in ‘experimental’ language version.

Experimental feature error - VS 2014

You can do this manually editing the project file.(I didn’t found any option to do this via Visual Studio.) You can open the *.csproj file in your favorite text editor and add the following line.

<LangVersion>experimental</LangVersion>

Now it will be like this

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
  <PlatformTarget>AnyCPU</PlatformTarget>
  <DebugSymbols>true</DebugSymbols>
  <DebugType>full</DebugType>
  <Optimize>false</Optimize>
  <OutputPath>bin\Debug\</OutputPath>
  <DefineConstants>DEBUG;TRACE</DefineConstants>
  <ErrorReport>prompt</ErrorReport>
  <WarningLevel>4</WarningLevel>
  <LangVersion>experimental</LangVersion>
</PropertyGroup>

Happy Programming :)

Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub