New Features in C# 6.0

download New Features in C# 6.0

of 14

Transcript of New Features in C# 6.0

  • 8/18/2019 New Features in C# 6.0

    1/14

    New Features in C# 6.0

  • 8/18/2019 New Features in C# 6.0

    2/14

    What to expect from this slide?

    Microsoft has announced some new keywords and behavior for C# 6.0 in Visual Studio 2015

    The features are listed as a comparison between C# 6.0 and C# 5.0

    Relevant samples are also provided to get a better understanding of the features.

    http://www.softwareassociates.in/http://www.softwareassociates.in/

  • 8/18/2019 New Features in C# 6.0

    3/14

    What are new features ?

    using Static

    Auto property initializer

    Dictionary Initializer

    nameof Expression

    New way for Exception filters

    Null – Conditional Operator Expression–Bodied Methods

    Easily format strings – String interpolation

    http://www.softwareassociates.in/http://www.softwareassociates.in/

  • 8/18/2019 New Features in C# 6.0

    4/14

    1) using static

    C# 6.0 allows us to use any class that is static as a namespace. This is very useful for every developer w

    the static methods from a static class

    1) Convert.ToInt32()

    2) Console.WriteLine()

    e.g. code in C 5.0

    Console.WriteLine(“Enter First Number : “);

    int num1 = Convert.ToInt32(Console.ReadLine());

    How to write the same code in C 6.0

    using System.Console;

    using System.Convert;

    WriteLine(“Enter First Number : ”); int mum1 = ToInt32(ReadLine());

    http://www.softwareassociates.in/http://www.softwareassociates.in/

  • 8/18/2019 New Features in C# 6.0

    5/14

    1) using static (cont’d)

    C# 5.0 Screenshot C# 6.0 Screenshot

    http://www.softwareassociates.in/http://www.softwareassociates.in/

  • 8/18/2019 New Features in C# 6.0

    6/14

    2) Auto property initializer

    C# 5.0 C# 6.0

    Auto property initializer is a new concept to set the value of a property during property declaration. We

    value to a read-only property, it means a property that has only a {get;} attribute.

    http://www.softwareassociates.in/http://www.softwareassociates.in/

  • 8/18/2019 New Features in C# 6.0

    7/14

    3) Dictionary Initializer

    In C# 6.0, the dictionary initialization happens in the same way but with keys as indexers which makes the

    read and understandable. Here is a piece of code to show you how the new initializing feature can be use

    almost similar to how you access them e.g. errorCodes [1] = “Ex000001”.

    Here in C# 6.0 we can initialize the Dictionary values directly by the “=” operator and in C# 5.0 we need t

    object as a {key,value} pair and the output will be the same in both versions.

    Dictionary Initialization is not a new thing in C#. It was present since C# 3.0, but in C# 6.0 Microsoft enha

    adding a new ability to use the key/indexer to map with the dictionary values directly during the initializat

    The following code snippet shows how dictionary initializer were used prior to C# 6.0 to initialize dictiona

    with key and values. This is still valid and you can use it as earlier.

    http://www.softwareassociates.in/http://www.softwareassociates.in/

  • 8/18/2019 New Features in C# 6.0

    8/14

    3) Dictionary Initializer (cont’d)

    C# 6C# 5

    http://www.softwareassociates.in/http://www.softwareassociates.in/

  • 8/18/2019 New Features in C# 6.0

    9/14

    4) nameof - Expression

    nameof expression will return a string literal of the name of a property or a method .

    http://www.softwareassociates.in/http://www.softwareassociates.in/

  • 8/18/2019 New Features in C# 6.0

    10/14

    5) Exception filters

    Exception filters allow us to specify a condition with a catch block. So the catch block is executed only if tsatisfied.

    http://www.softwareassociates.in/http://www.softwareassociates.in/

  • 8/18/2019 New Features in C# 6.0

    11/14

    6) Null-Conditional Operator

    Suppose we need to write a code to compare the objects of an employee class with null-conditional op

    will need to write multiple lines of code using if() and else. In C# 6.0, we can rewrite the same code using

    The following sample code shows how to check the null value of an object in C# 6.0

    http://www.softwareassociates.in/http://www.softwareassociates.in/

  • 8/18/2019 New Features in C# 6.0

    12/14

    7) Expression–Bodied Methods

    C#5.0 C#6.0

    An Expression–Bodied Method is a very useful way to write a function in a new way. By using the “=>“ l

    6.0, we can easily write methods with single line.

    http://www.softwareassociates.in/http://www.softwareassociates.in/

  • 8/18/2019 New Features in C# 6.0

    13/14

    8) Easily format strings using String interpolation

    To easily format a string value in C# 6.0 without any string.Format() method we can write a format fo

    useful and time saving process to define multiple string values by

    $“{ variable }”.

    http://www.softwareassociates.in/http://www.softwareassociates.in/

  • 8/18/2019 New Features in C# 6.0

    14/14

    SOFTWARE ASSOCIATES

    Software Associates “Mascot”, Zilla Bungalow Road, Nadakkavu,

    Kozhikode PIN 673011, Kerala, India

    Kozhikode +91 495 276 5837 | Bangalore +91 984 700 5656 |

    London +44 797 340 0804

    REACH OUT TO US

    http://www.softwareassociates.in/http://www.softwareassociates.in/http://facebook.com/SAIndia01http://facebook.com/SAIndia01http://twitter.com/SA4WebDevhttp://twitter.com/SA4WebDevhttps://plus.google.com/+SoftwareassociatesIndiahttps://plus.google.com/+SoftwareassociatesIndia