Hassan Hashemi`s blog

Afghan software developers

Exploring C# 6.0 new features

a few weeks ago i was reading about some of the new features coming with C# 6 in different places. i`v decided to gather them so you can check them out at once if have`nt 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.

so lets get started:

1- $ sign:

 and 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>()
                $first = "Hassan"

            //Assign value to member
                                                                                                                            //the new way:
            col.$first = "Hassan";

            //the old 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 satisfiedthis one is my favorite feature, so lets look at an example:
                throw new ApplicationException("Me");
            catch (ApplicationException ex) if (ex.Message == "You")
                // this one won't execute.
            catch (ApplicationException 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 middle of an expression. it is as simple as that but really destroys a pain. i have been doing a lot of asp.net 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 declareation place. 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 constructor will help destroy the pain of capturing values of constructor parameters to fields in the class for further operations. this is really useful.
so the main purpose for it is using constructor parameters for initialization.
when declare a primary constructor all other constructors must call the primary constructor using :this().
here is an example finally:
     //this is the primary constructor:                                                                             public 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.

Comments (1) -

  • D3v

    8/20/2014 10:48:24 AM | Reply

    C# is going to be awesome for me