C# 6 Summary

Short and incomplete notes on Rob Conery Pluralsight course “Exploring C# 6 with Jon Skeet”

  • Auto-properties
    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;}


    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

  • Using Static
    Old way


    New way

    using static System.Math;
    using static SomeEnum

  • 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


  • 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.