C 6 Cheat Sheet

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

C# 6 Cheat Sheet By: Mosh Hamedani

programmingwithmosh.com

Auto-property initializers
public  class  Post  

{

       public  Collection<Comment>  Comments  {  get;  private  set;  }  

                               =  new  Collection<Comment>();

}  

Dictionary initializers
var  dictionary  =  new  Dictionary<string,  string>

{

       [“LAX”]  =  “Los  Angeles  Int’l  Airport”

}  

String interpolation
var  fullName  =  $”{firstName}  {lastName}”;  

Null-conditional operator
int?  count  =  post?.Tags?.Count;  
int  count  =  post?.Tags?.Count  ??  0;  

Getter-only auto-properties
public  class  Post  

{

         public  int  Votes  {  get;  }  //  no  “private  set”

}


Expression bodied members


public  class  Post  

{

       public  int  DaysOld  =>  (DateCreated  -­‐  DateTime.Today).TotalDays;

}


You might also like