Auto Properties
Existing Method
public class Employee
{
public string CompanyName{get;set;}
public string Location{get;}//readonly
public string Email{get;set;}
public int ConatctNo{get;set;}
}
Can't able set the Default Value.
New Method
public class Employee{
public string CompanyName{get;set;} = "Company Name";
public string Location{get;}="Location";//readonly
public string Email{get;set;}="email@domail.com";
public int ConatctNo{get;set;}=123456789;
}
Static Classes
Existing Method
using System.Math;public class calculation
{
public int SquareNumber(int x)
{
return Math.Sqrt(x);
}
}
New Method
using System.Math;public class calculation
{
public int SquareNumber(int x)
{
return Sqrt(x); //No need to use Static Class Name.
}
}
String Formatting
Existing Method
return String.Format("{1} {0}.",FirstName,LastName);New Method
return "\{LastName} \{FirstName}.";Also Use Lambda Expression
()=>{return "\{LastName} \{FirstName}.";}or
()=>"\{LastName} \{FirstName}.";
Index initalizers
Existing Method
public JObject returnJSON()
{
var objJSON=new JObject();
objJSON["key1"]="value 1";
objJSON["key2"]="value 2";
return objJSON;
}
New Method
public JObject returnJSON(){
return new JObject() { ["key1"]="value 1", ["key2"]="value 2" }
}
or
In Lambda Express
public JObject returnJSON() => new JObject() { ["key1"]="value 1", ["key2"]="value 2" };Index initalizers for Dictionary
Existing Method
Dictionarydictionary.Add("key2", 2);
dictionary.Add("key1", 1);
dictionary.Add("key0", 0);
dictionary.Add("keyN1", -1);
int value = dictionary["key2"];
New Method
Dictionary{
["key2"]=2),
["key1"]=1),
["key0"]=0),
["keyN1"]=-1)
}
or
Dictionary{
$key2=2,
$key1=1,
$key0=0,
$keyN1=-1
}
int value = dictionary.$key
Null Conditional Operators
Existing Method
public string getStringValueJSON(json,key){
if(json != null && json[key] != null && json[key].Type == JTokenType.String)
{
return json[key].ToString();
}
return null;
}
New Method
public string getStringValueJSON(json,key){
if(json?.[key]?.Type == JTokenType.String)
{
return json[key].ToString();
}
return null;
}
Null Conditional Operators For Delegate Object
Existing Method
{var onChange=OnChange();
if(onChange != null)
onChange(this,args);
}
New Method
{OnChange?.Invoke(this,args);
}
"nameof" operator
Existing Method
if(objData==null)throw new ArgumentNullException("objData");
New Method
if(objData==null)throw new ArgumentNullException(nameof(objData));
Exception filters
Existing Method
try{
....
}catch(ConfigureException e)
{
...
}
New Method
try{
....
}catch(ConfigureException e) if(e.IsServer)
{
...
}
Await in catch and finally block
Existing Method
we can't use await inside the catch and finally blockNew Method
try{
....
}
catch(Exception ex)
{
await logException(ex);
}
finally
{
await closeConnection();
}
Happy Code...
No comments:
Post a Comment