Introduction
I would like to give a quick understanding about the C# 6.0 features which are newly introduced and improved feature which were being used in the earlier version of C# along with simple code example.
Using the Code
(i) Auto Property Initialization
It is improved & very easy for all developers now. We can initialize auto property while defining itself.
For example:
public sting DeptName
{ get;set;} = "Engineering";
If we want to initialize Read-Only auto property, we can do it either in property definition or constructor.
First way, (while defining property):
public string CustomerName
{ get; } = "Rajan" ;
Second way, (in constructor):
public string CustomerName
{ get; }
public Customer(string name)
{
this.CustomerName = name;
}
(ii) "nameof()" Operator
Earlier:
private string deptname;
public string DeptName
{
get {return deptname;}
set {
deptname =value;
OnPropertyChanged("DeptName");
}
}
nameof(propertyname)
returns string
lateral for the given propertyname
.
private string deptname;
public string DeptName
{
get {return deptname;}
set {
deptname =value;
OnPropertyChanged(nameof(DeptName)); }
}
(iii) Await Usage in Catch (or) Finally Block
List<MyLog> logs=new List<MyLog>();
try
{
logs=await GetMyLogs();
}
catch(Exception ex)
{
await GetExceptionLogs(logs,ex);
}
finally
{
if(logs!=null)
await CloseAllLogs();
}
Now, we will be able to use await
keyword inside catch
/ finally
block also. Earlier, it was restricted to use only in try
block.
(iv) Exception Filter
Syntax
try
{}
catch(Exception ex) if(condition)
{
}
Example
try { }
catch(Exception ex) if(e.InnerException != null)
{
}
The “if
” condition should be before the open parenthesis of catch()
block. Catch
block will be executed only when if loop returns true
, else it will move on to next statement or catch()
, if we have one more.
(v) Expression Bodied Members
If we’re having read-only property, method and user defined operators, where the method body has a expression like single return statement, then we can use this feature like lambda expression.
We can use “=>” instead parenthesis only when there is a single return statement.
If we consider, OrderName
is the read-only property and product order (private
field) is the member of the Product
class, then we can write like below:
public string OrderName => this.productOrder;
private void Display() => WriteLine("Hi This is superb feature..");
Here, this is not a lambda expression.
(vi) Null Propagation
If we say, Person
class has Address
property, then person
(p
) is the instance of Person
class. Earlier, we followed the way given below:
if (p != null && p.PersonAddress != null)
var state=p.PersonAddress.State;
Now null
checking is very easy by using (?.
):
var address= p?.p.PersonAddress??
”Has no data”;
(vii) String Interpolation
The below improved version of string
interpolation works only in the preview of Visual Studio 2015. In a later release, there may be a change in this feature.
Earlier:
Console.WriteLine("{0}", person.Name);
Now:
WriteLine("\{person.Name}");
We will be able to use alignment and format specifier as well:
var s = "\{p.Name,20} is \{p.Age:D3} year{s} old";
Directly, we will be able to use property name inside the braces.
(viii) Dictionary Initializer
Earlier:
Dictionary<string, string> dictionaryInitializer = new Dictionary<string, string>()
{
{"Label Text", "Enter Name"},
{"Button Content", "Click to Go"}
};
But now:
Dictionary<string, string> dictionaryInitializer = new Dictionary<string, string>()
{["Label Text"] ="Enter Name",["Button Content"] ="Click to Go”};
The above code helps to initialize not only members but also indices of newly created object.
(ix) Using Static Members
Through this feature, we can import static
members of type into project and custom type static
can also be imported.
Remember one thing, this feature is only apt for members of static type.
Well known example:
using System;
int a=45;
Console.WriteLine(Math.Tan(a));
Changed to:
using System.Console;
using System.Math;
int a=45;
WriteLine(Tan(a));
Hope this feature will make developers' life very easy.