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

What's new in C# 6

What's new in C# 6

My take on what's new in C# 6.

396eb98ecde29fe1fc8bcddbbf398f8f?s=128

Runar Ovesen Hjerpbakk

January 13, 2016
Tweet

Transcript

  1. E N A B L I N G E F

    F I C I E N T H E A L T H C A R E C# 6 – What’s new Runar Ovesen Hjerpbakk Software Engineering Manager hjerpbakk.com - @hjerpbakk - roh@dips.no 12.01.2015
  2. E N A B L I N G E F

    F I C I E N T H E A L T H C A R E  Simpler code  Clearer intention  Roslyn! – Loose ends tied up – CaaS – Cross-platform goodness  https://github.com/Sankra/WhatsNewInCSharp6 My take on C# 6
  3. E N A B L I N G E F

    F I C I E N T H E A L T H C A R E  Properties – Initializers for auto-properties – Getter-only auto-properties  Expression-bodied members  Using static  nameof expressions  Null-conditional operator  String interpolation  Improved Initializers  Exception filters  Await in catch and finally blocks Broad strokes
  4. E N A B L I N G E F

    F I C I E N T H E A L T H C A R E  Initializers for auto-properties  Getter-only auto-properties  Less code  Simpler to make really immutable objects  Initializers do not use set, always sets the backing field directly  Initializers cannot access this Properties
  5. E N A B L I N G E F

    F I C I E N T H E A L T H C A R E  When is the keyword implicit used in C#? C# 1 Quiz
  6. E N A B L I N G E F

    F I C I E N T H E A L T H C A R E  The implicit keyword is used to declare an implicit user-defined type conversion operator. Use it to enable implicit conversions between a user-defined type and another type, if the conversion is guaranteed not to cause a loss of data. Answer
  7. E N A B L I N G E F

    F I C I E N T H E A L T H C A R E  Expression bodies on method-like members  Expression bodies on property-like function members  The curly-brace-remover-feature  Improves readability for one-liner methods  Also useful in getter-only properties  Must be a statement expression in methods returning void or Task Expression-bodied members
  8. E N A B L I N G E F

    F I C I E N T H E A L T H C A R E  Imports all available static members  Limit visibility of extension methods by using the type, not the namespace  Useful for static classes with a lot of constants or functions  Or when you use a lot of enum values  The imported class does not need to be static  System.Math is the canonical usage Using static
  9. E N A B L I N G E F

    F I C I E N T H E A L T H C A R E  What does the attribute [CallerMemberName] do? C# 5 Quiz
  10. E N A B L I N G E F

    F I C I E N T H E A L T H C A R E Answer
  11. E N A B L I N G E F

    F I C I E N T H E A L T H C A R E  The compiler writes the name of the given identifier  Simplifies refactoring and prevents errors  Useful in – ArgumentNullExceptions – PropertyChanged-events – Log statements  Can dot to identifier, but only the final one will be used nameof expressions
  12. E N A B L I N G E F

    F I C I E N T H E A L T H C A R E  Guards against null with an easy, clean syntax  Short-circuits the dot-chain  Can be chained themselves  An invocation cannot follow the ? Operator, use explicit Invoke  Thread safe way for checking events for null Null-conditional operator
  13. E N A B L I N G E F

    F I C I E N T H E A L T H C A R E  More readable string formats  Expressions right in their place in the string.Format  Alignment and format specifiers can still be used  Content can be any expression, including other strings String interpolation
  14. E N A B L I N G E F

    F I C I E N T H E A L T H C A R E  How does explicitly implementing a member of an interface differ from a regular interface implementation in C#? C# 1 Quiz
  15. E N A B L I N G E F

    F I C I E N T H E A L T H C A R E  When a member is explicitly implemented, it cannot be accessed through a class instance, but only through an instance of the interface.  Also enables inheritance from two interfaces with the same member.  Can also make a public implementation of a method return a value which is more specific than specified in an interface. Answer
  16. E N A B L I N G E F

    F I C I E N T H E A L T H C A R E  Set values to keys through any indexer  The Add method used can now be an extension method  Only members supported previously  Brings objects with indexers up to par Improved Initializers
  17. E N A B L I N G E F

    F I C I E N T H E A L T H C A R E  Catch specific exceptions of a given type  Other exceptions of the same type ignores the catch with the stack unharmed  Simplifies rethrowing exceptions you don’t care about while preserving the stack  Can be used for sideeffects  VB had it, now we have it too Exception filters
  18. E N A B L I N G E F

    F I C I E N T H E A L T H C A R E  Further finishing async and await implementation, now works as expected in exception handling Await in catch and finally blocks
  19. E N A B L I N G E F

    F I C I E N T H E A L T H C A R E 1. Getter-only auto-properties 2. Null-conditional operator 3. Expression-bodied members 4. Await in catch and finally blocks 5. nameof expressions 6. String interpolation 7. Using static 8. Exception filters 9. Initializers for auto-properties 10. Improved Initializers My rankings of usefulness