Sei sulla pagina 1di 14

New Features in C# 6.

What to expect from this slide?


01

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.

What are new features ?


02

using Static
Auto property initializer
Dictionary Initializer
nameof Expression
New way for Exception filters
Null Conditional Operator
ExpressionBodied Methods
Easily format strings String interpolation

1) using static
03

C# 6.0 allows us to use any class that is static as a namespace. This is very useful for every developer where we need to call
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());

1) using static (contd)


04
C# 5.0 Screenshot

C# 6.0 Screenshot

2) Auto property initializer


05
Auto property initializer is a new concept to set the value of a property during property declaration. We can set default
value to a read-only property, it means a property that has only a {get;} attribute.
C# 5.0

C# 6.0

3) Dictionary Initializer
06
Dictionary Initialization is not a new thing in C#. It was present since C# 3.0, but in C# 6.0 Microsoft enhanced this by
adding a new ability to use the key/indexer to map with the dictionary values directly during the initialization itself.
The following code snippet shows how dictionary initializer were used prior to C# 6.0 to initialize dictionary objects
with key and values. This is still valid and you can use it as earlier.

In C# 6.0, the dictionary initialization happens in the same way but with keys as indexers which makes the code easy to
read and understandable. Here is a piece of code to show you how the new initializing feature can be used. This is
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 to create an
object as a {key,value} pair and the output will be the same in both versions.

3) Dictionary Initializer (contd)


07
C# 5

C# 6

4) nameof - Expression
08
nameof expression will return a string literal of the name of a property or a method.

5) Exception filters
09
Exception filters allow us to specify a condition with a catch block. So the catch block is executed only if the condition is
satisfied.

6) Null-Conditional Operator
10
Suppose we need to write a code to compare the objects of an employee class with null-conditional operator in C# 5.0, we
will need to write multiple lines of code using if() and else. In C# 6.0, we can rewrite the same code using ? and ?? .
The following sample code shows how to check the null value of an object in C# 6.0

7) ExpressionBodied Methods
11
An ExpressionBodied Method is a very useful way to write a function in a new way. By using the => lamda Operator in C#
6.0, we can easily write methods with single line.

C#5.0

C#6.0

8) Easily format strings using String interpolation


12

To easily format a string value in C# 6.0 without any string.Format() method we can write a format for a string. It's a very
useful and time saving process to define multiple string values by
${ variable }.

REACH OUT TO US
13

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

Potrebbero piacerti anche