C# 6.0 New Feature
C# 6.0 New Feature
How to get C# Updated Version?
Two ways we can update C# 6.0
Updated Visual Studio into VS 2014
Or
Installing Roslyn Package in VS 2013
$ Sign :
Its simplify String indexingExample:
var value = new Dictionary() {// using inside the initialize
$first = "Nikhil"
}
Assign value to member
C# 5.0
|
C# 6.0
|
Value[first] =”Nikhile”
|
Value. $first = “Nikhil”
|
Exception Filters:
Exception filter already support in Vb.net now C#.net as well start to support .Exception filter is nothing but in catch block we can specify If Condition.IF condition is fail inside catch block statement it won’t execute.
C# 5.0
|
C# 6.0
|
try
{ }
catch (Exception ex)
{ }
|
Try {
throw new Exception("Null Error");
}
catch (Exception ex) if (ex.Message == " IndexError")
{
// this one will not execute.
}
catch (Exception ex) if (ex.Message == " Null Error ")
{
// this one will execute
}
|
await in Catch block and finally block :
C# 5.0
|
C# 6.0
|
try
{
DoSomething();
}
catch (Exception)
{
//Its not Support here
await LogService.LogAsync(ex);
}
|
try
{
DoSomething();
}
catch (Exception)
{
// New feature it will support
await LogService.LogAsync(ex);
}
|
Declaration Expression:
C# 5.0
|
C# 6.0
|
Int Qty;
Int.TryParse(txtnumber.Text, out Qty)
|
Int.TryParse(txtnumber.Text, Int out Qty)
|
Auto Property Initialize:
{
// You can use this feature on both
//getter only and setter / getter only properties
public string FirstName { get; set; } = "Nikhil";
public string LastName { get; } = "Jagathesh";
}
Primary constructors:
simple example:
//this is primary constructorpublic Class Money(string currency, decimal amount)
{
public string Currency { get; } = currency;
public decimal Amount { get; } = amount;
}
Dictionary Initialize:
C# 5.0
|
C# 6.0
|
Dictionary
{
{ "India", "TN" },
{ "United States", "Washington" },
{ "Some Country", "Some Capital " }
};
|
Dictionary
{
// Look at this!
["Afghanistan"] = "Kabul",
["Iran"] = "Tehran",
["India"] = "Delhi"
};
|
Null-Conditional Operator
Look below new feature very interesting
C# 5.0
|
C# 6.0
|
public static string Truncate(string value, int length)
{
string result = value;
//this pain work for us
if (value != null) // Skip empty string check for elucidation
{
result = value.Substring(0, Math.Min(value.Length, length));
}
return result;
}
|
public static string Truncate(string value, int length)
{
return value?.Substring(0, Math.Min(value.Length, length));
}
|
0 Comments