What's new in C # 6.0?

Microsoft has released a preview version of Visual studio 2015 and .Net 4.6 for developers. The new C # 6.0 has several new features that can facilitate coding.
This article discusses the new features of C # 6.0. You can download the new VS here:
Microsoft Visual Studio Ultimate 2015 Preview
Initializing Properties with Values
In C # 6.0, we can initialize properties with values by writing their value to the right of them. This will help to avoid an error with null and empty property values.
Earlier:
publicint Id { get; set; }
publicstring FirstName { get; set; }
Now:
publicint Id { get; set; } = 1001;
publicstring FirstName { get; set; } = "Srinivas";
Line interpolation
Every day we have to deal with string concatenation. Someone mainly uses the “+” operator, while others use the string.Format () method. I personally like string.Format (). But everyone knows the problems with it: with too many parameters it’s hard to understand what each number means - {1}, {2}, {3}. C # 6.0 came up with a new feature that should combine the virtues of both methods.
Earlier:
name = string.Format("Employee name is {0}, located at {1}", emp.FirstName, emp.Location);
Now:
name = $"Employee name is {emp.FirstName}, located at {emp.Location}";
At the request of workers IL code
IL_0000: nop
IL_0001: ldstr "Ivan"
IL_0006: stloc.0
IL_0007: ldstr "Moscow"
IL_000c: stloc.1
IL_000d: ldstr "Employee name is {0}, located at {1}"
IL_0012: ldloc.0
IL_0013: ldloc.1
IL_0014: call string [mscorlib]System.String::Format(string, object, object)
IL_0019: stloc.2
IL_001a: ret
You can also use the conditions:
name = $"Employee name is {emp.FirstName}, located at {emp.Location}. Age of employee is
{(emp.Age > 0) ? emp.Age.ToString() : "N/A"}";
Using lambda expressions
In C # 6.0, properties and methods can be defined through lambda expressions. This greatly reduces the amount of code.
Earlier:
publicstring[] GetCountryList()
{
returnnewstring[] { "Russia", "USA", "UK" };
}
Now:
publicstring[] GetCountryList() => newstring[] { "Russia", "USA", "UK" };
Import Static Classes
All static members of a class can be defined using another static class. But we have to constantly repeat the name of this static class. With a large number of properties, you have to repeat the same thing many times.
C # 6.0 introduced the ability to import using the keyword using static classes. Let's look at an example of using the Math library:
Earlier
double powerValue = Math.Pow(2, 3);
double roundedValue = Math.Round(10.6);
Now:
usingSystem.Math;
double powerValue = Pow(2, 3);
double roundedValue = Round(10.6);
This can be used not only inside the class, but also when executing the method:
Earlier:
var employees = listEmployees.Where(i => i.Location == "Bangalore");
Now:
usingSystem.Linq.Enumerable;
var employees = Where(listEmployees, i => i.Location == "Bangalore");
Null conditional statement
C # 6.0 introduces a new so-called Null-conditional operator (?.) That will work on top of the conditional operator (? :). Its purpose is to facilitate checking for NULL values.
It returns null values if the class object to which the operator is applied is null:
var emp = new Employee()
{
Id = 1,
Age = 30,
Location = "Bangalore",
Department = new Department()
{
Id = 1,
Name = "IT"
}
};
Earlier:
string location = emp == null ? null : emp.Location;
string departmentName = emp == null ? null : emp.Department == null ? null : emp.Department.Name;
Now:
string location = emp?.Location;
string departmentName = emp?.Department?.Name;
nameof operator
In C # 6.0, the nameof operator will be used to avoid the appearance of string property literals in the code. This operator returns the string literal of the element passed into it. You can pass any member of the class or the class itself as a parameter.
var emp = new Employee()
{
Id = 1,
Age = 30,
Location = "Moscow",
Department = new Department()
{
Id = 1,
Name = "IT"
}
};
Response.Write(emp.Location); //result: Moscow
Response.Write(nameof(Employee.Location)); //result: Location
Await in catch and finally blocks
Prior to C # 6.0, you could not use the await operator in catch and final blocks. Now such an opportunity has appeared. It can be used to free resources or to maintain error logs.
publicasync Task StartAnalyzingData()
{
try
{
// код
}
catch
{
await LogExceptionDetailsAsync();
}
finally
{
await CloseResourcesAsync();
}
}
Exception filters
Exception filters were in the CLR, and they are available in VB, but they were not in C #. Now this feature has appeared, and you can impose an additional filter on exceptions:
try
{
//Вызываем исключение
}
catch (ArgumentNullException ex) if (ex.Source == "EmployeeCreation")
{
//Нотификация об ошибке
}
catch (InvalidOperationException ex) if (ex.InnerException != null)
{
//Нотификация об ошибке
}
catch (Exception ex) if (ex.InnerException != null)
{
//Сохраняем данные в лог
}
Dictionary Initialization
C # 6.0 added the ability to initialize a Dictionary by a value key. This should simplify the initialization of dictionaries.
For example, for JSON objects:
var country = new Dictionary<int, string>
{
[0] = "Russia",
[1] = "USA",
[2] = "UK",
[3] = "Japan"
};
C # 6.0 has a lot of syntax changes and new features. Microsoft is also improving the new compiler in terms of performance.
PS New features are described on the current version of the compiler, the syntax may change by the release of the final version.