Click here to Skip to main content
15,439,290 members
Articles / Programming Languages / C#
Posted 18 Aug 2014


56 bookmarked

Briefly exploring C# 6 new features

Rate me:
Please Sign up or sign in to vote.
4.65/5 (80 votes)
20 Aug 2014CPOL3 min read
briefly exploring most important new features of C# 6

A few weeks ago i was reading about some of the new features coming with C# 6 in different places. I've decided to gather them so you can check them out at once if haven't already. some of them may not as amazing as expected but that the update for now.

You can get them by downloading VS 2014 or installing the Roslyn package for visual studio 2013 from here.


i add new items to the Article once they are added to Roslyn download package or remove them once they are removed from Roslyn download package.

So lets get started:

1. $ sign

 The purpose for it is to simplify string based indexing and that is all. its not some thing like current dynamic features of  C# since it internally uses regular indexing functionality. to understand lets consider following example:
  var col = new Dictionary<string, string>()
                // using inside the intializer
                $first = "Hassan"

   //Assign value to member

   //the old way:
   col["first"] = "Hassan";
   // the new way   
   col.$first = "Hassan";

2. Exception filters:

Exception filters are already supported in VB compiler but now they are coming into C#. exception filters lets you specify a condition for a catch block. the catch block gets executed only if the condition is satisfied, this one is my favorite feature, so let's look at an example:
    throw new Exception("Me");
catch (Exception ex) if (ex.Message == "You")
    // this one will not execute.
catch (Exception ex) if (ex.Message == "Me")
    // this one will execute

3. await in catch and finally block:

As far as I know, no one know why in C# 5 using await keyword in catch and finally block was not available, any way its now possible to use though. this is great because often we want to perform I/O operation in order to log the exception reason in catch block or such things in finally block and they Need asynchrony.
catch (Exception)
    await LogService.LogAsync(ex);

4. Declaration expressions

This feature simply allows you to declare local variable in the middle of an expression. It is as simple as that but really destroys a pain. I have been doing a lot of web form projects in the past and this was my every day code:
long id;
if (!long.TryParse(Request.QureyString["Id"], out id))
{ }
which can be improved to this:
if (!long.TryParse(Request.QureyString["Id"], out long id))
{ }
The scoping rules for this kind of declaration is same as general scoping rules in C#.

5. 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 ConsoleApplication10
    class Program
        static void Main(string[] args)
            //Use writeLine method of Console class
            //Without specifying the class name
            WriteLine("Hellow World");

6. Auto property initializer:

With C# 6 initialize auto properties just like a field at declaration place. The only thing to notice here is that this initialization dose not cause the setter function to be invoked internally. the value of backing field is set directly. so here is an example:
public class Person
        // You can use this feature on both
        //getter only and setter / getter only properties

        public string FirstName { get; set; } = "Hassan";
        public string LastName { get; } = "Hashemi";

7. Primary Constructor:

Woohooo, primary constructors will help destroy the pain of capturing values of constructor parameters to fields in the class for further operations. This is really useful. The main purpose for it is using constructor parameters for initialization. When declaring a primary constructor all other constructors must call the primary constructor using :this().
here is an example finally:
//this is the primary constructor:
class Person(string firstName, string lastName)
    public string FirstName { get; set; } = firstName;
    public string LastName  { get; } = lastName;
notice that declaration of primary constructor is at the top of the class.

8- Dictionary Initializer:

some people believed that the old way of initiailzing dictionaries was dirty so the C# team dicided to make it cleaner, thanks to them. here is an example of the old way and new way:

// the old way of initializing a dictionary
Dictionary<string, string> oldWay = new Dictionary<string, string>()
    { "Afghanistan", "Kabul" },
    { "United States", "Washington" },
    { "Some Country", "Some Capital city" }

// new way of initializing a dictionary
Dictionary<string, string> newWay = new Dictionary<string, string>()
    // Look at this!
    ["Afghanistan"] = "Kabul",
    ["Iran"] = "Tehran",
    ["India"] = "Delhi"


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Afghanistan Afghanistan
you can always find me at

Comments and Discussions

GeneralMy vote of 5 Pin
Nidhin David23-May-15 16:02
MemberNidhin David23-May-15 16:02 
QuestionMore Explanation on certain topics needed. Pin
ManojSridhar9129-Mar-15 19:02
MemberManojSridhar9129-Mar-15 19:02 
QuestionTry these features online Pin
r@hu!3-Feb-15 17:21
Memberr@hu!3-Feb-15 17:21 
SuggestionExcellent Article. Pin
Anjum.Rizwi17-Dec-14 17:53
professionalAnjum.Rizwi17-Dec-14 17:53 
QuestionNice Article. Pin
khanqamarali10-Dec-14 5:07
Memberkhanqamarali10-Dec-14 5:07 
GeneralMy vote of 5 Pin
Khayralla25-Nov-14 6:38
MemberKhayralla25-Nov-14 6:38 
QuestionPrimary constructors and declaration expressions seem to be excluded Pin
Wendelius2-Oct-14 7:54
mveWendelius2-Oct-14 7:54 
GeneralMy vote of 5 Pin
Sooraj_Singh11-Sep-14 20:53
professionalSooraj_Singh11-Sep-14 20:53 
QuestionExcellent article Pin
rdunaway11-Sep-14 4:23
Memberrdunaway11-Sep-14 4:23 
Question$ sign Pin
Dave Doknjas8-Sep-14 18:07
MemberDave Doknjas8-Sep-14 18:07 
AnswerRe: $ sign Pin
Vitaly Tomilov14-Sep-14 12:27
MemberVitaly Tomilov14-Sep-14 12:27 
Questionlong.TryParse("9", out long id); -problem Pin
Michael Pauli25-Aug-14 5:18
MemberMichael Pauli25-Aug-14 5:18 
AnswerRe: long.TryParse("9", out long id); -problem Pin
Hassan Hashemi25-Aug-14 6:50
MemberHassan Hashemi25-Aug-14 6:50 
GeneralRe: long.TryParse("9", out long id); -problem Pin
Michael Pauli25-Aug-14 23:13
MemberMichael Pauli25-Aug-14 23:13 
GeneralRe: long.TryParse("9", out long id); -problem Pin
Hassan Hashemi26-Aug-14 0:17
MemberHassan Hashemi26-Aug-14 0:17 
QuestionGrammar, grammar, grammar. Pin
exyyle24-Aug-14 5:45
professionalexyyle24-Aug-14 5:45 
AnswerRe: Grammar, grammar, grammar. Pin
Hassan Hashemi24-Aug-14 18:54
MemberHassan Hashemi24-Aug-14 18:54 
AnswerRe: Grammar, grammar, grammar. Pin
Michael Pauli25-Aug-14 23:21
MemberMichael Pauli25-Aug-14 23:21 
GeneralRe: Grammar, grammar, grammar. Pin
exyyle26-Aug-14 6:03
professionalexyyle26-Aug-14 6:03 
AnswerRe: Grammar, grammar, grammar. Pin
celalit2-Oct-14 16:17
Membercelalit2-Oct-14 16:17 
AnswerRe: Grammar, grammar, grammar. Pin
Anjum.Rizwi17-Dec-14 18:06
professionalAnjum.Rizwi17-Dec-14 18:06 
QuestionNull objects Pin
Graeme_22-Aug-14 5:20
MemberGraeme_22-Aug-14 5:20 
AnswerRe: Null objects Pin
LloydA11123-Aug-14 5:54
MemberLloydA11123-Aug-14 5:54 
GeneralRe: Null objects Pin
Graeme_23-Aug-14 7:07
MemberGraeme_23-Aug-14 7:07 
QuestionInteresting article. Some nice time savers, but... Pin
Graeme_22-Aug-14 5:19
MemberGraeme_22-Aug-14 5:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.