c# today and tomorrow

21
C# Today & Tomorrow

Upload: bertrand-le-roy

Post on 22-Jan-2018

1.486 views

Category:

Engineering


0 download

TRANSCRIPT

Page 1: C# Today and Tomorrow

C# Today & Tomorrow

Page 2: C# Today and Tomorrow

http://stackoverflow.com/research/developer-survey-2015

Page 3: C# Today and Tomorrow

http://stackoverflow.com/research/developer-survey-2015

Page 4: C# Today and Tomorrow

http://stackoverflow.com/research/developer-survey-2015

Page 5: C# Today and Tomorrow

Changing our tune…

Run on Windows

.NET as system component

Run on VM (CLR)

Black box compilers

Edit in Visual Studio

Proprietary

Run everywhere

Deploy with app

Compile to native

Open compiler APIs

Use your favorite editor

Open source

Page 6: C# Today and Tomorrow

Project Roslyn

“Let’s rewrite the C# and VB compilers and IDE from scratch!”

• Evolve

• Enable

• Dogfood

Page 7: C# Today and Tomorrow

The compiler/IDE dichotomy

Compiler

Batch

Throughput

Correctness

Prevent badness

Reactive to what you did

IDE

Incremental

Responsiveness

Error tolerance

Enable goodness

Predictive of what you will do

Page 8: C# Today and Tomorrow

The Roslyn Compiler API

There should only need to be

one code base in the world

for understanding C#

Page 9: C# Today and Tomorrow

Other IDEs On other platforms

Custom diagnostics Style, API usage, patterns…

Source transformation Refactorings, fixers, migraters, updaters…

Scripting Batch scripts, hosted code…

Coding in execution REPL, edit-and-continue…

Documentation Hyperlinked API docs, web playgrounds…

Static analysis Code metrics, graphs, telemetry, code querying…

Metaprogramming Source generators, injectors, weavers…

Other language understanding scenarios

Page 10: C# Today and Tomorrow

Custom analyzers and fixes

Plug in to diagnostic reporting and code fixing infrastructure

Batch and interactive

“Code-aware libraries”

APIs can ship with analyzers and fixes to guide their users

Toolboxes

Style enforcement, discovery of opportunities

Page 11: C# Today and Tomorrow

Language evolution explained

Stagnation

C#

Page 12: C# Today and Tomorrow

Evolution of C#

C# 1

Hello World

C# 2

Generics

C# 3Queries, Lambdas

C# 4Dynamic, Concurrency

C# 5

Async

C# 6Avoid boilerplate

C# 7

???

Page 13: C# Today and Tomorrow

Demo: REPL and C# 6

Page 14: C# Today and Tomorrow

C# “7”

Page 15: C# Today and Tomorrow

Pattern matching

if (o is Point p) { WriteLine($"({p.X}, {p.Y})"); }

if (o is Point p && p.X == 5) { WriteLine($"Y: {p.Y}"); }

if (o is Point(5, var y)) { WriteLine($"Y: {y}"); }

Page 16: C# Today and Tomorrow

Patterns in switch statements

switch (o){

case int i:WriteLine($"Number {i}");break;

case Point(int x, int y):WriteLine($"({x},{y})");break;

case string s when s.Length > 0:WriteLine(s);break;

case null:WriteLine("<null>");break;

default:WriteLine("<other>");break;

}

Page 17: C# Today and Tomorrow

Tuples

public (int sum, int count) Tally(IEnumerable<int> values)

{

var s = 0; var c = 0;

foreach (var value in values) { s += value; c++; }

return (s, c);

}

var t = Tally(myValues);

Console.WriteLine($"Sum: {t.sum}, count: {t.count}");

(var s, var c) = Tally(myValues);

Console.WriteLine($"Sum: {s}, count: {c}");

Page 18: C# Today and Tomorrow

Records

class Person : IEquatable<Person>{

public string First { get; }public string Last { get; }

public Person(string First, string Last) { this.First = First; this.Last = Last; }

public (string First, string Last) Deconstruct() => (First, Last);

public bool Equals(Person other) => First == other.First && Last == other.Last;

public override bool Equals(object obj) => obj is Person other ? Equals(other) : false;public override int GetHashCode() => GreatHashFunction(First, Last);

…}

class Person(string First, string Last);

Page 19: C# Today and Tomorrow

Creating immutable objects

var p1 = new Point { X = 3, Y = 7 };

var p2 = p1 with { X = -p1.X };

Page 20: C# Today and Tomorrow

Nullable and non-nullable reference types

string? n; // Nullable reference type

string s; // Non-nullable reference type

n = null; // Sure; it's nullable

s = null; // Warning! Shouldn’t be null!

s = n; // Warning! Really!

WriteLine(s.Length); // Sure; it’s not null

WriteLine(n.Length); // Warning! Could be null!

if (n != null) { WriteLine(n.Length); } // Sure; you checked

WriteLine(n!.Length); // Ok, if you insist!

Page 21: C# Today and Tomorrow

?