Upgrade to Pro — share decks privately, control downloads, hide ads and more …

What's New in C#6

Avatar for Jason Bock Jason Bock
February 29, 2016

What's New in C#6

Dissatisfied that C#5 only added async and await? In C#6 you now have a lot more features! In this session we'll cover what these features are (e.g. nameof and string interpolation) and how they work in detail so you'll know when to use them effectively in your new .NET projects. You'll also see where C# is going with proposed features for C#7.

Avatar for Jason Bock

Jason Bock

February 29, 2016
Tweet

Other Decks in Programming

Transcript

  1. »Classes (and Interfaces) and Structs »Common Control Flow (switch, if/else,

    etc.) »Exception Handling (try/catch/finally) »Visibility (public/protected/private) »Delegates and Events Language Evolution Version 1
  2. »LINQ › Type Inference › Object and Collection Initializers ›

    Anonymous Types › Expression Trees › Extension Methods Language Evolution Version 3
  3. Language Evolution http://www.dotnetfoundation.org/ The .NET Foundation is an independent organization

    to foster open development and collaboration around the growing collection of open source technologies for .NET
  4. »Auto-property initializers »Getter-only auto-properties »Ctor assignment to getter-only autoprops »Using

    static members »Indexer initializer »Await in catch/finally »Exception filters Language Evolution Version 6 https://github.com/dotnet/roslyn/wiki/Languages-features-in-C%23-6-and-VB-14 »Expression-bodied members »Null propagation »String interpolation »nameof operator »#pragma »Extension Add in collection initializers »Improved overload resolution
  5. »Auto-property initializers › Makes it easy to set a property

    to an initial value › public property FirstName { get; set; } = "Jason"; »Getter-only auto-properties › If you want a property to be truly “read-only” – i.e. a private setter and the backing field is marked as readonly… › public property FirstName { get; } = "Jason"; »Ctor assignment to getter-only autoprops › You can also do this with getter-only autoprops › public Person { this.FirstName = "Jason"; } C#6 Features - Properties
  6. »Using static members › Static members always require the name

    of the class for resolution › using “using static” removes that › E.g. - using static System.Math; › This also “imports” all the extension methods as well »Expression-bodied members › If a method is short – e.g. one line of code – you can now define without the curly braces › E.g. public int Double(int x) => Add(x, x); C#6 Features - Functions
  7. »Null conditional › Enables safe access to members in deep

    object graphs › E.g. - parent.Child?.ToString() »String interpolation › Allows you to define the “holes” directly in a formattable string › E.g. - $"{this.FirstName} {this.LastName}" »nameof › Provides a way to get the name of a member in code › Removes the need for magic string usage › Useful for ArgumentNullException, property changed implementations, WeakEventManager, etc. C#6 Features – Syntax
  8. »Await in catch/finally › Before C#6, you couldn’t await a

    method in a catch or finally block. Now you can! › E.g. – try { /* ... */ } catch(MyException) { await SomeCall(); } »Exception filters › VB has always had the ability to filter exceptions – that is, only enter a catch block if some condition is true. Now C#6 has it! › E.g. – catch(MyException) when (this.HandleIt) { /* ... */ } C#6 Features - Exceptions
  9. »Indexer initializer › Can be used for dictionaries or any

    object with an indexer › E.g. – var things = new Things { [0] = "Tesla" }; »#pragma › Now works with an identifier as well as an integer, which maps to C# errors › E.g. these are the same − #pragma warning disable 219 − #pragma warning disable CS0219 › Potentially useful for analyzer errors to disable/enable rather than using SuppressMessageAttribute everywhere C#6 Features - Miscellaneous
  10. »My choices/ideas › Generic attributes: [MyMetadata<string>] › Fault blocks: try

    { … } fault { … } › Metaprogramming: [Disposable] public class MyClass › Reactive programming Future Directions
  11. »C#7 (maybe) › Ref locals and returns › Local methods

    › Slices › Metaprogramming › Tuples (not just the Tuple type) › Pattern matching › Non-nullable types Future Directions
  12. What’s New in C#6 Jason Bock Practice Lead Remember… •

    https://github.com/JasonBock/WhatsNewInCSharp6 • http://www.slideshare.net/jasonbock/whats-new-in-c6 • References in the notes on this slide