Short and incomplete notes on Rob Conery Pluralsight course “Exploring C# 6 with Jon Skeet”
- Auto-properties
Immutability
Old way (still can set anywhere within the class)public string Name {get; private set;}
New way (can be set only in a constructor or initializer)
public string Name{get;}
Initializerspublic string Name{get;} = "J";
- Expression-bodied members
For Properties
Old waypublic Timespan Age{ get {return DateTime.UtcNow - DateOfBirth;}
New way
public Timespan Age => return DateTime.UtcNow - DateOfBirth;
For Methods
Old waypublic int GetName() {return "Joe";}
New way
public int GetName() => "Joe";
- nameof
Old way
Use string literal; expression tree, or [CallerMemberName] attribute
New way
nameof(propertyName) - Using Static
Old wayMath.Sin(x) SomeEnum.EnumName
New way
using static System.Math; using static SomeEnum ... Sin(x) EnumName
- String Interpolation
$”{SomePropertyOrExpression} …” - Nullability
Null Conditional Operator
Old wayreturn a!=null && a.b!=null && a.b.c=="abc";
New way
return a!.b!.c=="abc";
Event Handlers
Old wayvar handler = eventHandler; if (handler !=null) {handler...}
New way
eventHandler?.Invoke...
- Exception Filters
catch(WebException e) when(e.Status == ...)