Slide 1

Slide 1 text

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 - [email protected] 12.01.2015

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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