Upload
software-associates
View
232
Download
0
Embed Size (px)
Citation preview
01
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.
02
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
03
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 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());
04
1) using static (cont’d)
C# 5.0 Screenshot C# 6.0 Screenshot
05
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 can set default value to
a read-only property, it means a property that has only a {get;} attribute.
06
3) Dictionary Initializer
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.
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.
07
3) Dictionary Initializer (cont’d)
C# 6C# 5
08
4) nameof - Expression
nameof expression will return a string literal of the name of a property or a method.
09
5) Exception filters
Exception filters allow us to specify a condition with a catch block. So the catch block is executed only if the condition is satisfied.
10
6) Null-Conditional Operator
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
11
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 “=>“ lamda Operator in C# 6.0, we
can easily write methods with single line.
12
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 for a string. It's a very useful and
time saving process to define multiple string values by
$“{ variable }”.
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
REACH OUT TO US