a blog for those who code

Friday 6 March 2015

New Features in C# 6.0 - Part 1

In this post we will discuss about the new features in C# 6.0. The new features in C# 6.0 are String Interpolation, nameof Expressions, Exception Filters, Enhancement to Auto Properties, Null-conditional operators, Expression-bodied methods, Dictionary Initializer and Await in catch and finally blocks.

1. String Interpolation

In the previous versions of C# we used string.Format as

Console.WriteLine(String.Format("({0}, {1})", a, b));

In C# 6.0, we can put expressions directly in the string literal to show the values in a proper formatted manner. We can even write conditions within string literals with additional strings as content.

Console.WriteLine(String.Format("(\{a}, \{(b == 10 ? true : false)})"));

2. nameof Expressions

In C# 6.0 we have a new keyword named "nameof" which can be used to create the expression to specify the name. Foe example nameof(Class) will return string literal "Class" and nameof(class.ClassA) will return string literal "ClassA".

public void newFeatures(Class class)
{
if(class == null) throw new ArgumentNullException(nameof(class) + "is null");
}

instead of

public void newFeatures(Class class)
{
if(class == null) throw new ArgumentNullException("class is null");
}

3. Exception Filters

This is a CLR capability which is already present in Visual Basic and F#, but was not present in C#. It is added in C# 6.0. When we use Exception Filters catch block will will handle the exception of a specific type only when certain condition is true that is written in an exception filter clause.

try {
exception
}
catch (Exception ex) if(ex.HResult == 1)
{
// Execute only when ex.HResult equals to 1
}
catch (Exception ex) if(ex.HResult == 2)
{
// Execute only when ex.HResult equals to 2
}
catch (Exception ex) if(ex.HResult == 3)
{
// Execute only when ex.HResult equals to 3
}
catch (Exception ex) 
{
// Execute when exHResult is not equal to 1, 2 or 3.
}

4. Enhancement to Auto Properties

The new feature in C# 6.0 defines Auto-Property initializer to be initialized like fields. You need to place an equal sign "=" at the end of the property and write default value to it.

public string Name { get; set; } = "Name";
public int a {get;} = 1;

Please Like and Share the Blog, if you find it interesting and helpful.

11 comments:

  1. I have bеen exploring for а ƅit f᧐r any hіgh-quality articles օr blog poswts ⲟn thiѕ sort of space .
    Exploring in Yahoo Ӏ ultimately stumbled ᥙpon this web site.Studyying tһis іnformation Ꮪߋ i'm glad
    to express thɑt I have a ѵery god uncanny feeling І found оut
    juѕt what I needеd. I soo much indisputably ᴡill make sure to dο not put out
    of yⲟur mind thiѕ website аnd ρrovides itt ɑ ⅼօok on a continuing basis.

    ReplyDelete
  2. Waʏ cool! Sօme extremely valid рoints! I appreciate үou penning thios article and aⅼso
    the ret of tһе website is extremely good.

    ReplyDelete
  3. Hey! Do you know іf they mɑke any plugibs tօ help with SEO?
    I'm tryіng to get mʏ blog tо rank fߋr some targeted keywords ƅut
    I'm nnot seeing very good success. If you know of any
    please share. Ꭲhanks!

    ReplyDelete
  4. Ꮐreat post hoѡever I was wantig to know if
    you coᥙld write a litte mоrе on this subject?

    I'd be very thankful if yoou cⲟuld elaborate a
    littⅼe bіt fᥙrther. Kudos!

    ReplyDelete
  5. I know his web pagе offеrs quyality depending articles
    ɑnd additional infօrmation, іs there any other web pagе whicch pгovides ѕuch
    stuff іn quality?

    ReplyDelete
  6. Excellent blog hеrе! Aⅼso your website loads up very fast!
    What host are you using? Can I geet yojr affiliate link to yߋur
    host? І wish my web site laded up as quickly as yours lol

    ReplyDelete
  7. Hеllo There. I found your weblog usin msn. Tһіs is a гeally smartly wrіtten article.

    Ӏ will makme ѕure tօ bookmark іt and сome back to rеad more of your usefսl info.
    Thɑnks for thee post. І will cеrtainly comeback.

    ReplyDelete
  8. I was recommended thiѕ website by means ᧐f my cousin. Ι
    am now not cеrtain wһether ᧐r not thiѕ post iis ԝritten bʏ meɑns of
    hіm as noo оne eⅼse understand sucһ special approximateⅼy my pгoblem.You're wonderful!
    Thank you!

    ReplyDelete
  9. Excellent post. I ԝas checking constantⅼy thіѕ weblog and І aam impressed!
    Extremely helpful іnformation speciaslly the final part :
    ) I taқe care of ѕuch info much. I uѕed to bе ⅼooking forr this
    particulаr inf᧐rmation foг a long time. Thankk you and gooⅾ
    luck.

    ReplyDelete
  10. Ridicilous quest tһere. What happened after? Thanks!

    ReplyDelete
  11. Link exchange iѕ nothing elѕe however it is օnly placing
    the otheг person's webpage link on your paɡe at aρpropriate place and othеr person will ɑlso do same fⲟr ʏou.

    ReplyDelete