C# 6.0 - DotNetNotts
-
Upload
citizenmatt -
Category
Engineering
-
view
72 -
download
1
Transcript of C# 6.0 - DotNetNotts
![Page 1: C# 6.0 - DotNetNotts](https://reader033.fdocument.pub/reader033/viewer/2022052602/55c40310bb61eb65078b473f/html5/thumbnails/1.jpg)
C# 6.0 Features and Tooling
Matt Ellis@citizenmatt
![Page 2: C# 6.0 - DotNetNotts](https://reader033.fdocument.pub/reader033/viewer/2022052602/55c40310bb61eb65078b473f/html5/thumbnails/2.jpg)
Back storyRoslynLanguage features
![Page 3: C# 6.0 - DotNetNotts](https://reader033.fdocument.pub/reader033/viewer/2022052602/55c40310bb61eb65078b473f/html5/thumbnails/3.jpg)
HISTORY
![Page 4: C# 6.0 - DotNetNotts](https://reader033.fdocument.pub/reader033/viewer/2022052602/55c40310bb61eb65078b473f/html5/thumbnails/4.jpg)
C# 1.0 (2002)
Initial commit
C# 1.2 (2003)Really!?
![Page 5: C# 6.0 - DotNetNotts](https://reader033.fdocument.pub/reader033/viewer/2022052602/55c40310bb61eb65078b473f/html5/thumbnails/5.jpg)
C# 2.0 (2005)
GenericsNullable types
IteratorsPartial typesAnonymous methodsGetter/setter accessibilityStatic classes
New CLR features
Language features
![Page 6: C# 6.0 - DotNetNotts](https://reader033.fdocument.pub/reader033/viewer/2022052602/55c40310bb61eb65078b473f/html5/thumbnails/6.jpg)
C# 3.0 (2007)
Anonymous typesImplicit typing (“var”)Extension methodsLambda expressionsQuery expressionsExpression trees
Auto propertiesObject and collection initialisersPartial methods
LINQ
![Page 7: C# 6.0 - DotNetNotts](https://reader033.fdocument.pub/reader033/viewer/2022052602/55c40310bb61eb65078b473f/html5/thumbnails/7.jpg)
C# 4.0 (2010)
DynamicNamed and optionalparametersEmbedded interop types
Co- and contra-variance for generics
COM interop
![Page 8: C# 6.0 - DotNetNotts](https://reader033.fdocument.pub/reader033/viewer/2022052602/55c40310bb61eb65078b473f/html5/thumbnails/8.jpg)
C# 5.0 (2012)
Async/awaitCaller Info attributes
private async static Task<string> DownloadText(string url){ using (var webClient = new WebClient()) return await webClient.DownloadStringTaskAsync(url);}
![Page 9: C# 6.0 - DotNetNotts](https://reader033.fdocument.pub/reader033/viewer/2022052602/55c40310bb61eb65078b473f/html5/thumbnails/9.jpg)
C# 6.0 (2015)
Roslyn
Auto property initialisersIndex initialisersUsing static membersNull propagationExpression bodied membersIEnumerable paramsnameof operator
Await in catch/finallyException filters
Ceremony
New compiler!
![Page 10: C# 6.0 - DotNetNotts](https://reader033.fdocument.pub/reader033/viewer/2022052602/55c40310bb61eb65078b473f/html5/thumbnails/10.jpg)
ROSLYN
![Page 11: C# 6.0 - DotNetNotts](https://reader033.fdocument.pub/reader033/viewer/2022052602/55c40310bb61eb65078b473f/html5/thumbnails/11.jpg)
Roslyn
“.NET Compiler Platform”Complete redesign & rewrite of compilerC# vs C++Decade old codebaseEase of adding new featuresParallel compilationOpen Source (github.com/dotnet/roslyn)
![Page 12: C# 6.0 - DotNetNotts](https://reader033.fdocument.pub/reader033/viewer/2022052602/55c40310bb61eb65078b473f/html5/thumbnails/12.jpg)
Compiler as a service
Compiler exposed as an APICan host compiler in-processCan compile in-memoryCan affect compilation steps, e.g. resolving references (but no meta-programming)Can access parse trees and semantic model
![Page 13: C# 6.0 - DotNetNotts](https://reader033.fdocument.pub/reader033/viewer/2022052602/55c40310bb61eb65078b473f/html5/thumbnails/13.jpg)
IDE features
Compiler powers syntax highlighting, navigation, etc.Parser and semantic model available across the IDEAbstract Syntax Tree based analysis, navigation and refactoring
![Page 14: C# 6.0 - DotNetNotts](https://reader033.fdocument.pub/reader033/viewer/2022052602/55c40310bb61eb65078b473f/html5/thumbnails/14.jpg)
ScriptCS
C# based REPL and scriptingScript packs and NuGet references
![Page 15: C# 6.0 - DotNetNotts](https://reader033.fdocument.pub/reader033/viewer/2022052602/55c40310bb61eb65078b473f/html5/thumbnails/15.jpg)
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
![Page 16: C# 6.0 - DotNetNotts](https://reader033.fdocument.pub/reader033/viewer/2022052602/55c40310bb61eb65078b473f/html5/thumbnails/16.jpg)
Meta-programming. Kinda.
Problem:
CLR types defined by assembly qualified nameEverything based on binary referencesContracts 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
![Page 17: C# 6.0 - DotNetNotts](https://reader033.fdocument.pub/reader033/viewer/2022052602/55c40310bb61eb65078b473f/html5/thumbnails/17.jpg)
Assembly neutral interfaces
Ideal:
Duck typing/structural equivalenceCommon contracts defined in source, resolved at runtime
But how do you work around assembly qualified name?
![Page 18: C# 6.0 - DotNetNotts](https://reader033.fdocument.pub/reader033/viewer/2022052602/55c40310bb61eb65078b473f/html5/thumbnails/18.jpg)
Compile time// Compiled to CommonLogging.ILogger.dllnamespace CommonLogging{ [AssemblyNeutral] public interface ILogger { void Log(string value); }}
Finds and removes [AssemblyNeutral] contract typesCompile a new assembly for each typePredictable assembly qualified nameOriginal assembly references the types in new assembliesNew assemblies saved as embedded resources in original assemblyMultiple assemblies defining same types generate same assemblies
![Page 19: C# 6.0 - DotNetNotts](https://reader033.fdocument.pub/reader033/viewer/2022052602/55c40310bb61eb65078b473f/html5/thumbnails/19.jpg)
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”
![Page 20: C# 6.0 - DotNetNotts](https://reader033.fdocument.pub/reader033/viewer/2022052602/55c40310bb61eb65078b473f/html5/thumbnails/20.jpg)
LANGUAGE FEATURES
![Page 21: C# 6.0 - DotNetNotts](https://reader033.fdocument.pub/reader033/viewer/2022052602/55c40310bb61eb65078b473f/html5/thumbnails/21.jpg)
Language features
Auto property initialisersIndex initialisersUsing static membersNull propagationExpression bodied membersIEnumerable paramsnameof operatorAwait in catch/finallyException filters
![Page 22: C# 6.0 - DotNetNotts](https://reader033.fdocument.pub/reader033/viewer/2022052602/55c40310bb61eb65078b473f/html5/thumbnails/22.jpg)
Auto property initialisation
Proper readonly (immutable) auto properties – getter onlyNo 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; }}
![Page 23: C# 6.0 - DotNetNotts](https://reader033.fdocument.pub/reader033/viewer/2022052602/55c40310bb61eb65078b473f/html5/thumbnails/23.jpg)
Index initialisers
Nicer syntax than previous collection initialisersCalls indexer rather than Add methodExtends object initialiser
var d = new Dictionary<string, int>{ ["doodad"] = 42, ["widget"] = 64};
![Page 24: C# 6.0 - DotNetNotts](https://reader033.fdocument.pub/reader033/viewer/2022052602/55c40310bb61eb65078b473f/html5/thumbnails/24.jpg)
Using static membersUse static members without qualificationApplies to methods, properties, fields, events, etc.Does not apply to extension methodsLocal 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); } }}
![Page 25: C# 6.0 - DotNetNotts](https://reader033.fdocument.pub/reader033/viewer/2022052602/55c40310bb61eb65078b473f/html5/thumbnails/25.jpg)
nameof operator
Returns short name of symbolAny 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));}
![Page 26: C# 6.0 - DotNetNotts](https://reader033.fdocument.pub/reader033/viewer/2022052602/55c40310bb61eb65078b473f/html5/thumbnails/26.jpg)
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;
![Page 27: C# 6.0 - DotNetNotts](https://reader033.fdocument.pub/reader033/viewer/2022052602/55c40310bb61eb65078b473f/html5/thumbnails/27.jpg)
Null propagation
Conditional access operatorReturns null if qualifier is null, else evaluatesShort circuitingConditional method invocation, indexer accessLeft hand side evaluated only once – thread safe
int? length = customers?.Length;int length2 = customers?.Length ?? 0;
![Page 28: C# 6.0 - DotNetNotts](https://reader033.fdocument.pub/reader033/viewer/2022052602/55c40310bb61eb65078b473f/html5/thumbnails/28.jpg)
Await in catch/finally
Implementation detailPreviously “impossible”IL cannot branch into or out of catch/finallyUses ExceptionDispatchInfo to save exception state and rethrow if necessary
![Page 29: C# 6.0 - DotNetNotts](https://reader033.fdocument.pub/reader033/viewer/2022052602/55c40310bb61eb65078b473f/html5/thumbnails/29.jpg)
Exception filters
Existing CLR featureOnly run catch block if exception handler returns trueReplaces if () throw; patternExpected “abuse” – logging
catch (Exception e) when (Log(e)){ // ...}
private bool Log(Exception exception){ Debug.WriteLine(exception); return false;}
![Page 30: C# 6.0 - DotNetNotts](https://reader033.fdocument.pub/reader033/viewer/2022052602/55c40310bb61eb65078b473f/html5/thumbnails/30.jpg)
String interpolation
$"My name is {name} and I’m {person.Age} years old"
Compiles to string.FormatHoles can contain expressions – e.g. $"Hi {GetName(person)}"Uses current culture by defaultFormat specifiers – e.g. $"{foo:D}”Does not work with stored resource stringsVerbatim strings can contain strings. What!?
![Page 31: C# 6.0 - DotNetNotts](https://reader033.fdocument.pub/reader033/viewer/2022052602/55c40310bb61eb65078b473f/html5/thumbnails/31.jpg)
Formattable strings
Interpolated strings converted to FormattableString by FormattableStringFactory.CreatePassed to method that returns a string formatted as required, e.g. Invariant culture, URL encoding, SQLProvide own FormattableString implementations on downlevel versions
![Page 32: C# 6.0 - DotNetNotts](https://reader033.fdocument.pub/reader033/viewer/2022052602/55c40310bb61eb65078b473f/html5/thumbnails/32.jpg)
Summary
RoslynComplete rewriteEasier to maintainOpen SourceCompiler as a service
IDE features, scripting, hosting compilers
Language featuresAuto property initialisersGetter only auto propertiesIndex initialisersUsing static membersNull propagationExpression bodied membersnameof operatorAwait in catch/finallyException filters
![Page 33: C# 6.0 - DotNetNotts](https://reader033.fdocument.pub/reader033/viewer/2022052602/55c40310bb61eb65078b473f/html5/thumbnails/33.jpg)
Bonus – C# 7.0
Strong interestTuplesPattern matchingRecordsNon-nullable typesAsync streams (observables) + disposable
Some interestCovariant return typesExpanded expression treesSyntax for lists + dictionariesDeterministic disposalImmutable typesType providersScriptingMethod contractsyield foreachparams IEnumerablemore...