C# 6 Summary

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;}
    

     
    Initializers

    public string Name{get;} = "J";
    
  •  

  • Expression-bodied members
    For Properties
    Old way

    public Timespan Age{ get {return DateTime.UtcNow - DateOfBirth;}
    

    New way

    public Timespan Age => return DateTime.UtcNow - DateOfBirth;
    

     
    For Methods
    Old way

    public 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 way

    Math.Sin(x)
    SomeEnum.EnumName
    

    New way

    using static System.Math;
    using static SomeEnum
    ...
    Sin(x)
    EnumName
    
  •  

  • String Interpolation
    $”{SomePropertyOrExpression} …”
  •  

  • Nullability
    Null Conditional Operator
    Old way

    return a!=null && a.b!=null && a.b.c=="abc";
    

    New way

    return a!.b!.c=="abc";
    

     
    Event Handlers
    Old way

    var handler = eventHandler;
    if (handler !=null) {handler...}
    

    New way

    eventHandler?.Invoke...
    
  •  

  • Exception Filters
    catch(WebException e) when(e.Status == ...)
    

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.