1. Implicit property initialization
public MyProperty {get;set;} = 10;
2. Access "Static" members directly by referring a type in "Using"
Using Console;
static void main(string[] args)
{
WritleLine("Happy Coding"); //earlier we wrote Console.WriteLine and did use "Using Console"
}
3. Implicitly mark "setter" to private
public string MyReadOnlyProperty {get;};
In C# 6.0 this would assume that "setter" is private. Earlier we had to explicitly declare that "setter" is private, for example
public string MyReadOnlyProperty {get; private set;}
4. "Out" can be declared in the expression itself and can be scoped locally
public bool Validate(object objToValidate, out List<string> errors){ // validation is here for objToValidate }
Earlier we wrote
private List<string> errors = new List<string>();
public bool Validate(object objToValidate, out List<string> errors)
The scope of variable "errors" was valid outside of the method also. In C# 6.0, if variable is declared within expression then the scope is within the method only.
5. Exception with If statement
try{}
catch(Exception ex) if (//condition 1){}
catch(Exception ex) if (//condition 2){}
Now it is possible to filter catch statement by a given condition
6. Lambda like function expression
var age = 10;
public string MyProperty => String.Format("I am {1} years old",age);
public decimal GetRoundedValue (decimal digit) => Decimal.Round(digit,2);
You may define any property, member or function like this.
7. Define a function with IEnumerable type perameter with Param kewords
void MyMethod(param IEnumberable<string> names)
{
names.ForEach(n => Save(name));
}
MyMethod("John","Johnyy","Jordan","James");
Earlier only arrays allowed with "param" keyword
8. User separator to initialize any numerical variable for better readability
int population = 1_000_000_00;
It is possible to separate digits for better readability. As far as compiler is concerned, it does not make any difference. The compiler would ignore "_" placed after a first digit.
9. "nameof" can be used to extract a string value of name of any variable, type or method
public void TestMethod()
{
try{}
catch(exception ex)
{
Logger.Log(ex.message, nameof(TestMethod));
}
}
10. Another "String" manipulation with /{}
private age = 10;
public string GetAgeDescription()
{
return "I am /{age} year/({age == 1?"":"s"}) old";
//output
//if age is 1 - I am 1 year old; if age is 3 - I am 3 years old
}
11. NULL conditional operator "?."
public string GetStreetAddress(Address address)
{
return address?.StreetAddress; //if address is NULL then returns NULL otherwise value of StreetAddress property
}
public int NumberofKids(Parent parent)
{
return parent?.Numberofkids??0; //0 if parent is NULL otherwise value of NumberofKids property
}
Earlier it was required to add "If" block to check whether address is NULL or not to avoid possible "Object not set to a reference" error.
12. Use "await" in Catch or Finally
public void DoSomething()
{
try{}
catch(exception ex)
{
await Logger.LoginTextFile(ex.message);
}
finally
{
await Email.SendToAdmin("Operation completed");
}
}
13. Initialize a dictionary with []
private Dictionary<int, string> Names = new Dictionary<int, string>
{
[0] = "Joe",
[1] = "John",
[2] = "Johnny",
[3] = "Jordan"
}