SlideShare a Scribd company logo
C# 6.0 Features and Tooling
Matt Ellis
@citizenmatt
Back story
Roslyn
Language features
HISTORY
C# 1.0 (2002)
Initial commit
C# 1.2 (2003)
Really!?
C# 2.0 (2005)
Generics
Nullable types
Iterators
Partial types
Anonymous methods
Getter/setter accessibility
Static classes
New CLR features
Language features
C# 3.0 (2007)
Anonymous types
Implicit typing (“var”)
Extension methods
Lambda expressions
Query expressions
Expression trees
Auto properties
Object and collection initialisers
Partial methods
LINQ
C# 4.0 (2010)
Dynamic
Named and optional
parameters
Embedded interop types
Co- and contra-variance for generics
COM interop
C# 5.0 (2012)
Async/await
Caller Info attributes
private async static Task<string> DownloadText(string url)
{
using (var webClient = new WebClient())
return await webClient.DownloadStringTaskAsync(url);
}
C# 6.0 (2015)
Roslyn
Auto property initialisers
Index initialisers
Using static members
Null propagation
Expression bodied members
IEnumerable params
nameof operator
Await in catch/finally
Exception filters
Ceremony
New compiler!
ROSLYN
Roslyn
“.NET Compiler Platform”
Complete redesign & rewrite of compiler
C# vs C++
Decade old codebase
Ease of adding new features
Parallel compilation
Open Source (github.com/dotnet/roslyn)
Compiler as a service
Compiler exposed as an API
Can host compiler in-process
Can compile in-memory
Can affect compilation steps, e.g. resolving
references (but no meta-programming)
Can access parse trees and semantic model
IDE features
Compiler powers syntax highlighting, navigation, etc.
Parser and semantic model available across the IDE
Abstract Syntax Tree based analysis, navigation and
refactoring
ScriptCS
C# based REPL and scripting
Script packs and NuGet references
ASP.Net vNext
Compiler pipeline based on Roslyn
Compiles to in-memory assemblies
Edit file + refresh browser = compiled
No need to recompile whole project
New project system, based on NuGet
Controls assembly/package resolution
Meta-programming. Kinda.
Problem:
CLR types defined by assembly qualified name
Everything based on binary references
Contracts are hardcoded. No loose coupling
E.g.: want to use the same logger across application +
3rd party framework?
Must share contract (assembly), or provide adapters
Assembly neutral interfaces
Ideal:
Duck typing/structural equivalence
Common contracts defined in source, resolved at runtime
But how do you work around assembly qualified name?
Compile time
// Compiled to CommonLogging.ILogger.dll
namespace CommonLogging
{
[AssemblyNeutral]
public interface ILogger
{
void Log(string value);
}
}
Finds and removes [AssemblyNeutral] contract types
Compile a new assembly for each type
Predictable assembly qualified name
Original assembly references the types in new assemblies
New assemblies saved as embedded resources in original assembly
Multiple assemblies defining same types generate same assemblies
Run time
Application tries to resolve
neutral assembly
Host environment looks in
resources of all assemblies
Uses first match it finds
If multiple assemblies define
same type, same dll is
generated
Types with same namespace
and name “Just Work”
LANGUAGE FEATURES
Language features
Auto property initialisers
Index initialisers
Using static members
Null propagation
Expression bodied members
IEnumerable params
nameof operator
Await in catch/finally
Exception filters
Auto property initialisation
Proper readonly (immutable) auto properties – getter only
No need for constructor or backing field
public class Person
{
public string Name { get; }
public Address Address { get; } = new Address();
public Person(string name)
{
// Assignment to getter only properties
Name = name;
}
}
Index initialisers
Nicer syntax than previous collection initialisers
Calls indexer rather than Add method
Extends object initialiser
var d = new Dictionary<string, int>
{
["doodad"] = 42,
["widget"] = 64
};
Using static members
Use static members without qualification
Applies to methods, properties, fields, events, etc.
Does not apply to extension methods
Local declarations resolved first
using static System.Math;
public class Sphere
{
private readonly double radius;
public Sphere(double radius)
{
this.radius = radius;
}
public double Volume
{
get { return (3/4)*PI*Pow(radius, 3); }
}
}
nameof operator
Returns short name of symbol
Any symbol – class, class member, namespace,
type parameters, variables, etc.
Ambiguities are not an error
public void Foo(string value)
{
if (value == null)
throw new ArgumentNullException(nameof(value));
}
Expression bodied members
Replaces single line methods, properties, indexers,
operators, etc.
Familiar lambda arrow syntax
Properties are created as getter only
Can only be single expression
public string FullName => FirstName + " " + LastName;
Null propagation
Conditional access operator
Returns null if qualifier is null, else evaluates
Short circuiting
Conditional method invocation, indexer access
Left hand side evaluated only once – thread safe
int? length = customers?.Length;
int length2 = customers?.Length ?? 0;
Await in catch/finally
Implementation detail
Previously “impossible”
IL cannot branch into or out of catch/finally
Uses ExceptionDispatchInfo to save exception
state and rethrow if necessary
Exception filters
Existing CLR feature
Only run catch block if exception handler returns true
Replaces if () throw; pattern
Expected “abuse” – logging
catch (Exception e) when (Log(e))
{
// ...
}
private bool Log(Exception exception)
{
Debug.WriteLine(exception);
return false;
}
String interpolation
$"My name is {name} and I’m {person.Age} years old"
Compiles to string.Format
Holes can contain expressions – e.g.
$"Hi {GetName(person)}"
Uses current culture by default
Format specifiers – e.g. $"{foo:D}”
Does not work with stored resource strings
Verbatim strings can contain strings. What!?
Formattable strings
Interpolated strings converted to
FormattableString by
FormattableStringFactory.Create
Passed to method that returns a string
formatted as required, e.g. Invariant culture,
URL encoding, SQL
Provide own FormattableString implementations
on downlevel versions
Summary
Roslyn
Complete rewrite
Easier to maintain
Open Source
Compiler as a service
IDE features, scripting,
hosting compilers
Language features
Auto property initialisers
Getter only auto properties
Index initialisers
Using static members
Null propagation
Expression bodied members
nameof operator
Await in catch/finally
Exception filters
Bonus – C# 7.0
Strong interest
Tuples
Pattern matching
Records
Non-nullable types
Async streams (observables) +
disposable
Some interest
Covariant return types
Expanded expression trees
Syntax for lists + dictionaries
Deterministic disposal
Immutable types
Type providers
Scripting
Method contracts
yield foreach
params IEnumerable
more...

More Related Content

PDF
Why Java Sucks and C# Rocks (Final)
PPTX
Actors model in gpars
PDF
Effective Scala: Programming Patterns
PDF
Java 8 features
PDF
Effective Scala (JavaDay Riga 2013)
PDF
Java 8 Lambda Expressions & Streams
PPTX
Java 8 presentation
Why Java Sucks and C# Rocks (Final)
Actors model in gpars
Effective Scala: Programming Patterns
Java 8 features
Effective Scala (JavaDay Riga 2013)
Java 8 Lambda Expressions & Streams
Java 8 presentation

What's hot (20)

PPTX
Java 8 Lambda and Streams
PPT
C# basics
PDF
Introduction to Dart
PPT
Javascript
PPT
PPT
JavaScript: The Language
PPT
PPT
PPTX
Oop2010 Scala Presentation Stal
PPT
Javascript
PDF
(6) c sharp introduction_advanced_features_part_i
PPTX
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
PPTX
Lambda Expressions in Java 8
PDF
PDF
JavaScript Programming
PPTX
Qcon2011 functions rockpresentation_f_sharp
PPTX
Oop2011 actor presentation_stal
PPSX
Writing code that writes code - Nguyen Luong
PDF
Introduction to Swift
PDF
Streams in Java 8
Java 8 Lambda and Streams
C# basics
Introduction to Dart
Javascript
JavaScript: The Language
Oop2010 Scala Presentation Stal
Javascript
(6) c sharp introduction_advanced_features_part_i
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Lambda Expressions in Java 8
JavaScript Programming
Qcon2011 functions rockpresentation_f_sharp
Oop2011 actor presentation_stal
Writing code that writes code - Nguyen Luong
Introduction to Swift
Streams in Java 8
Ad

Viewers also liked (20)

PPTX
PPTX
New features in C# 6
PDF
Configuring SSL on NGNINX and less tricky servers
PPTX
NuGet Must Haves for LINQ
PPTX
Dynamic C#
PDF
C# features through examples
PPTX
Evolution of c# - by K.Jegan
PPTX
A Tour of EF Core's (1.1) Most Interesting & Important Features
PPTX
EF6 or EF Core? How Do I Choose?
PPTX
ASP.NET MVC 4 Request Pipeline Internals
PPTX
Dependency injection in asp.net core
PPTX
Functional Programming with C#
PPTX
Repository design pattern in laravel - Samir Poudel
PPTX
ASP.NET MVC 4 - Routing Internals
PPTX
Introduction the Repository Pattern
PDF
Day02 01 Advance Feature in C# DH TDT
PDF
Functional Programming in C# and F#
PDF
코드의 품질 (Code Quality)
PDF
Repository and Unit Of Work Design Patterns
PDF
Generic repository pattern with ASP.NET MVC and Entity Framework
New features in C# 6
Configuring SSL on NGNINX and less tricky servers
NuGet Must Haves for LINQ
Dynamic C#
C# features through examples
Evolution of c# - by K.Jegan
A Tour of EF Core's (1.1) Most Interesting & Important Features
EF6 or EF Core? How Do I Choose?
ASP.NET MVC 4 Request Pipeline Internals
Dependency injection in asp.net core
Functional Programming with C#
Repository design pattern in laravel - Samir Poudel
ASP.NET MVC 4 - Routing Internals
Introduction the Repository Pattern
Day02 01 Advance Feature in C# DH TDT
Functional Programming in C# and F#
코드의 품질 (Code Quality)
Repository and Unit Of Work Design Patterns
Generic repository pattern with ASP.NET MVC and Entity Framework
Ad

Similar to C# 6.0 - DotNetNotts (20)

PPTX
PPSX
C# 6.0 - April 2014 preview
PPTX
C#6 - The New Stuff
PPTX
.NET Foundation, Future of .NET and C#
PPT
Introduction to Csharp (C-Sharp) is a programming language developed by Micro...
PPT
Introduction-to-Csharp.ppt
PPT
Introduction-to-Csharp programacion orientada a objetos
PPT
Introduction-to-Csharp.ppt
PPT
Introduction to-csharp
PPTX
The Little Wonders of C# 6
PPTX
PPTX
C#.net evolution part 2
PPT
IntroToCSharpcode.ppt
PPTX
What's coming to c# (Tel-Aviv, 2018)
PDF
Introduction to c#
PDF
Introduction To Csharp
DOCX
C# 6.0
PDF
C# interview-questions
PDF
C# 7.x What's new and what's coming with C# 8
PPTX
C# Today and Tomorrow
C# 6.0 - April 2014 preview
C#6 - The New Stuff
.NET Foundation, Future of .NET and C#
Introduction to Csharp (C-Sharp) is a programming language developed by Micro...
Introduction-to-Csharp.ppt
Introduction-to-Csharp programacion orientada a objetos
Introduction-to-Csharp.ppt
Introduction to-csharp
The Little Wonders of C# 6
C#.net evolution part 2
IntroToCSharpcode.ppt
What's coming to c# (Tel-Aviv, 2018)
Introduction to c#
Introduction To Csharp
C# 6.0
C# interview-questions
C# 7.x What's new and what's coming with C# 8
C# Today and Tomorrow

More from citizenmatt (10)

PDF
I see deadlocks : Matt Ellis - Techorama NL 2024
PDF
How to Parse a File (NDC London 2018)
PDF
How to Parse a File (DDD North 2017)
PDF
Rider - Taking ReSharper out of Process
PDF
The how-dare-you-call-me-an-idiot’s guide to the .NET Standard (NDC London 2017)
PDF
.NET Core Blimey! Windows Platform User Group, Manchester
PDF
.NET Core Blimey! (Shropshire Devs Mar 2016)
PDF
.NET Core Blimey! (dotnetsheff Jan 2016)
PDF
.net Core Blimey - Smart Devs UG
PDF
.Net Core Blimey! (16/07/2015)
I see deadlocks : Matt Ellis - Techorama NL 2024
How to Parse a File (NDC London 2018)
How to Parse a File (DDD North 2017)
Rider - Taking ReSharper out of Process
The how-dare-you-call-me-an-idiot’s guide to the .NET Standard (NDC London 2017)
.NET Core Blimey! Windows Platform User Group, Manchester
.NET Core Blimey! (Shropshire Devs Mar 2016)
.NET Core Blimey! (dotnetsheff Jan 2016)
.net Core Blimey - Smart Devs UG
.Net Core Blimey! (16/07/2015)

Recently uploaded (20)

PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
Lecture Notes Electrical Wiring System Components
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
composite construction of structures.pdf
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
DOCX
573137875-Attendance-Management-System-original
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
Digital Logic Computer Design lecture notes
PPTX
Geodesy 1.pptx...............................................
PPTX
web development for engineering and engineering
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
additive manufacturing of ss316l using mig welding
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Foundation to blockchain - A guide to Blockchain Tech
Lecture Notes Electrical Wiring System Components
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
UNIT-1 - COAL BASED THERMAL POWER PLANTS
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Operating System & Kernel Study Guide-1 - converted.pdf
composite construction of structures.pdf
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
573137875-Attendance-Management-System-original
bas. eng. economics group 4 presentation 1.pptx
Digital Logic Computer Design lecture notes
Geodesy 1.pptx...............................................
web development for engineering and engineering
OOP with Java - Java Introduction (Basics)
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
additive manufacturing of ss316l using mig welding

C# 6.0 - DotNetNotts

  • 1. C# 6.0 Features and Tooling Matt Ellis @citizenmatt
  • 4. C# 1.0 (2002) Initial commit C# 1.2 (2003) Really!?
  • 5. C# 2.0 (2005) Generics Nullable types Iterators Partial types Anonymous methods Getter/setter accessibility Static classes New CLR features Language features
  • 6. C# 3.0 (2007) Anonymous types Implicit typing (“var”) Extension methods Lambda expressions Query expressions Expression trees Auto properties Object and collection initialisers Partial methods LINQ
  • 7. C# 4.0 (2010) Dynamic Named and optional parameters Embedded interop types Co- and contra-variance for generics COM interop
  • 8. C# 5.0 (2012) Async/await Caller Info attributes private async static Task<string> DownloadText(string url) { using (var webClient = new WebClient()) return await webClient.DownloadStringTaskAsync(url); }
  • 9. C# 6.0 (2015) Roslyn Auto property initialisers Index initialisers Using static members Null propagation Expression bodied members IEnumerable params nameof operator Await in catch/finally Exception filters Ceremony New compiler!
  • 11. Roslyn “.NET Compiler Platform” Complete redesign & rewrite of compiler C# vs C++ Decade old codebase Ease of adding new features Parallel compilation Open Source (github.com/dotnet/roslyn)
  • 12. Compiler as a service Compiler exposed as an API Can host compiler in-process Can compile in-memory Can affect compilation steps, e.g. resolving references (but no meta-programming) Can access parse trees and semantic model
  • 13. IDE features Compiler powers syntax highlighting, navigation, etc. Parser and semantic model available across the IDE Abstract Syntax Tree based analysis, navigation and refactoring
  • 14. ScriptCS C# based REPL and scripting Script packs and NuGet references
  • 15. ASP.Net vNext Compiler pipeline based on Roslyn Compiles to in-memory assemblies Edit file + refresh browser = compiled No need to recompile whole project New project system, based on NuGet Controls assembly/package resolution
  • 16. Meta-programming. Kinda. Problem: CLR types defined by assembly qualified name Everything based on binary references Contracts are hardcoded. No loose coupling E.g.: want to use the same logger across application + 3rd party framework? Must share contract (assembly), or provide adapters
  • 17. Assembly neutral interfaces Ideal: Duck typing/structural equivalence Common contracts defined in source, resolved at runtime But how do you work around assembly qualified name?
  • 18. Compile time // Compiled to CommonLogging.ILogger.dll namespace CommonLogging { [AssemblyNeutral] public interface ILogger { void Log(string value); } } Finds and removes [AssemblyNeutral] contract types Compile a new assembly for each type Predictable assembly qualified name Original assembly references the types in new assemblies New assemblies saved as embedded resources in original assembly Multiple assemblies defining same types generate same assemblies
  • 19. Run time Application tries to resolve neutral assembly Host environment looks in resources of all assemblies Uses first match it finds If multiple assemblies define same type, same dll is generated Types with same namespace and name “Just Work”
  • 21. Language features Auto property initialisers Index initialisers Using static members Null propagation Expression bodied members IEnumerable params nameof operator Await in catch/finally Exception filters
  • 22. Auto property initialisation Proper readonly (immutable) auto properties – getter only No need for constructor or backing field public class Person { public string Name { get; } public Address Address { get; } = new Address(); public Person(string name) { // Assignment to getter only properties Name = name; } }
  • 23. Index initialisers Nicer syntax than previous collection initialisers Calls indexer rather than Add method Extends object initialiser var d = new Dictionary<string, int> { ["doodad"] = 42, ["widget"] = 64 };
  • 24. Using static members Use static members without qualification Applies to methods, properties, fields, events, etc. Does not apply to extension methods Local declarations resolved first using static System.Math; public class Sphere { private readonly double radius; public Sphere(double radius) { this.radius = radius; } public double Volume { get { return (3/4)*PI*Pow(radius, 3); } } }
  • 25. nameof operator Returns short name of symbol Any symbol – class, class member, namespace, type parameters, variables, etc. Ambiguities are not an error public void Foo(string value) { if (value == null) throw new ArgumentNullException(nameof(value)); }
  • 26. Expression bodied members Replaces single line methods, properties, indexers, operators, etc. Familiar lambda arrow syntax Properties are created as getter only Can only be single expression public string FullName => FirstName + " " + LastName;
  • 27. Null propagation Conditional access operator Returns null if qualifier is null, else evaluates Short circuiting Conditional method invocation, indexer access Left hand side evaluated only once – thread safe int? length = customers?.Length; int length2 = customers?.Length ?? 0;
  • 28. Await in catch/finally Implementation detail Previously “impossible” IL cannot branch into or out of catch/finally Uses ExceptionDispatchInfo to save exception state and rethrow if necessary
  • 29. Exception filters Existing CLR feature Only run catch block if exception handler returns true Replaces if () throw; pattern Expected “abuse” – logging catch (Exception e) when (Log(e)) { // ... } private bool Log(Exception exception) { Debug.WriteLine(exception); return false; }
  • 30. String interpolation $"My name is {name} and I’m {person.Age} years old" Compiles to string.Format Holes can contain expressions – e.g. $"Hi {GetName(person)}" Uses current culture by default Format specifiers – e.g. $"{foo:D}” Does not work with stored resource strings Verbatim strings can contain strings. What!?
  • 31. Formattable strings Interpolated strings converted to FormattableString by FormattableStringFactory.Create Passed to method that returns a string formatted as required, e.g. Invariant culture, URL encoding, SQL Provide own FormattableString implementations on downlevel versions
  • 32. Summary Roslyn Complete rewrite Easier to maintain Open Source Compiler as a service IDE features, scripting, hosting compilers Language features Auto property initialisers Getter only auto properties Index initialisers Using static members Null propagation Expression bodied members nameof operator Await in catch/finally Exception filters
  • 33. Bonus – C# 7.0 Strong interest Tuples Pattern matching Records Non-nullable types Async streams (observables) + disposable Some interest Covariant return types Expanded expression trees Syntax for lists + dictionaries Deterministic disposal Immutable types Type providers Scripting Method contracts yield foreach params IEnumerable more...

Editor's Notes

  • #5: C# 1.0 released in 2002 C# 1.2 released for .net 1.1. Called Dispose on IEnumerators which also implemented IDisposable. A few other features Anonymous methods – code blocks for delegates. NOT lambdas
  • #6: C# 1.0 released in 2002 C# 1.2 released for .net 1.1. Called Dispose on IEnumerators which also implemented IDisposable. A few other features Anonymous methods – code blocks for delegates. NOT lambdas
  • #8: I can take *in* a larger type than I was requesting, e.g. IEnumerable<Cat> passed into IEnumerable<Animal>
  • #9: Caller info – CallerFilePathAttribute, CallerLineNumberAttribute, CallerMethodNameAttribute Couple of minor fixes to scoping of variables in foreach loops, and order of evaluation of named and positional parameters
  • #12: Open Source – taken contributions. Nothing major, but e.g. improvements to error messages
  • #14: In the old world – compiler was one parser, language services was another parser, and expression evaluator was a third. Roslyn unifies all of these, which is a good thing
  • #16: Lots of other changes – no msbuild project file, json for configuration, heavily intertwined with NuGet, based on OWIN, etc.
  • #24: Also, collection initialisers will now use Add extension methods
  • #25: Again, about brevity Some talk of substituting, e.g. Console.WriteLine for Debug.WriteLine, but don’t approve of that Useful for e.g. System.Math, Logger.Log Error if there’s an ambiguity e.g. using Console + Debug, and call WriteLine ReSharper: Good support – missing QF + CA Parses, code completion in using list, code completion for symbols, resolves, find usages, navigation Missing QF + CA, control flow redundant code in qualifier
  • #26: For getting rid of magic strings. E.g. Reflection, PropertyChanged, ArgumentException Stems from typeof and mythical infoof Short name. Drops type parameters. Last name of namespace Type parameter is unsubstituted. Aliases are unsubstituted Nameof(int) vs nameof(int32) Interesting questions for refactoring, as name can be ambiguous – what if I rename one of the symbols in the set? ReSharper support: None. Tries to resolve symbol called nameof namespace A.B.C { class C { } } namespace D.E.F { class C { } // same generic arity } namespace G.H.I { class C<T, U, V> { } } namespace X { using A.B.C; using D.E.F; using G.H.I; class X { // here 'C' reference means three different 'C' types // no 'ambiguous reference' error produced at all void M(string s) => M(nameof(C)); } }
  • #27: Must be a statement for void methods, so return value isn’t even ignored ReSharper support: Full Parses, resolves, find usages, control flow Inspection on property getter return statement CA on return statement of simple method CA on expression bodied member
  • #28: Evaluates left to right If it’s any step is null, returns null, short circuits *all* following steps Conditional method invocations can be skipped if null Lifts type to be a nullable type – use null coalescing to reduce back down to type Grouping with brackets (customers?[0]?).ToUpper() <- need better example Grouping with brackets break out of short circuiting uses result as qualifier (because using the access operator, not the conditional access operator) Cannot do delegate?(args), must do delegate?.Invoke(args) Thread safe due to LHS only evaluated once + stored in temporary variable, so useful for event handlers
  • #29: Where TryRoslyn is really nice. Can see exactly what the implementation is
  • #30: Order of filters is important Previous syntax was “if”, but that broke existing code with an if in a catch statement