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