C C # # 7, 7, 8, 8, and beyon ond: lang languag uage e fe - - PowerPoint PPT Presentation

c c 7 7 8 8 and beyon ond lang languag uage e fe features
SMART_READER_LITE
LIVE PREVIEW

C C # # 7, 7, 8, 8, and beyon ond: lang languag uage e fe - - PowerPoint PPT Presentation

C C # # 7, 7, 8, 8, and beyon ond: lang languag uage e fe features from design to to release to to IDE su support ort Kevin Pilch kevinpi@microsoft.com @Pilchie ck Overflow - most popular technologies Stack


slide-1
SLIDE 1

C C # # 7, 7, 8, 8, and beyon

  • nd:

lang languag uage e fe features from design to to release to to IDE su support

  • rt

Kevin Pilch kevinpi@microsoft.com @Pilchie

slide-2
SLIDE 2

http://stackoverflow.com/insights/survey/2017#most-popular-technologies

Stack ck Overflow - most popular technologies

slide-3
SLIDE 3

http://stackoverflow.com/insights/survey/2017#most-loved-dreaded-and-wanted

St Stack Overf rflow - most loved technologies

slide-4
SLIDE 4

Ch Changing the tune - Why you can use C#

Run on Windows Black box compilers Edit in Visual Studio Proprietary Run everywhere Open compiler APIs Use your favorite editor Open source

slide-5
SLIDE 5

C# C# everyw ywhere - Xamarin

slide-6
SLIDE 6

C# C# ev everywhere - Unity

slide-7
SLIDE 7

C# C# ev everywhere - .NET Core

slide-8
SLIDE 8

Ro Roslyn – the C# language engine

The here sho houl uld ne need d to be be onl nly

  • n
  • ne co

code base

in n the the world d for unde understandi nding ng C#

slide-9
SLIDE 9

Om OmniSharp – edit C# everywhere

slide-10
SLIDE 10

C# C# Evolution – A balancing act

Stay simple Improve existing development Stay true to the spirit of C# Aggressively improve Attractive to new users Embrace new paradigms

slide-11
SLIDE 11

C# C# - evolution

C# 1 Hello World C# 2 Generics C# 3 Queries, Lambdas C# 4 Dynamic, Concurrency C# 5 Async C# 6 Eliminate ceremony C# 7 Work with data

slide-12
SLIDE 12

C# C# 6 - recap

Expression bodied members String Interpolation Conditional access operator nameof Using static Exception Filters Interactive window

slide-13
SLIDE 13

Demo: C# 7.0

slide-14
SLIDE 14

C# C# 7. 7.0 0 – what’s new

static void Main(string[] args) {

  • bject[] numbers =

{ 0b1, 0b10, new object[] { 0b100, 0b1000 }, // binary literals 0b1_0000, 0b10_0000 }; // digit separators var (sum, count) = Tally(numbers); // deconstruction WriteLine($"Sum: {sum}, Count: {count}"); } static (int sum, int count) Tally(object[] values) // tuple types { var r = (s: 0, c: 0); // tuple literals void Add(int s, int c) { r.s += s, r.c += c; } // local functions return r; }

slide-15
SLIDE 15

C# C# 7.0 7.0 – what’s new

static (int sum, int count) Tally(object[] values) // tuple types { var r = (s: 0, c: 0); // tuple literals void Add(int s, int c) { r.s += s, r.c += c; } // local functions foreach (var v in values) { switch (v) // switch on any value { case int i: // type patterns Add(i, 1); break; case object[] a when a.Length > 0: // case conditions var t = Tally(a); Add(t.sum, t.count); break; } } return r; }

slide-16
SLIDE 16

C# C# 7.0 7.0 – what’s new

Binary literals Digit separators Tuples Patterns Local functions “Tasklike” async methods Out “var” “ref” locals and returns Throw expressions More expression bodied members

slide-17
SLIDE 17

C# C# 7.1 7.1 – introducing point releases

“default” expressions Async main Infer tuple names Reference assemblies

slide-18
SLIDE 18

C# C# 7.2, 7.2, 8.0, 8.0, … … – long term thinking

“ref readonly” “blittable” Interior pointer (needed for Span<T>) Nullable Reference Types Default Interface methods

slide-19
SLIDE 19

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!

slide-20
SLIDE 20

Default implementations

public interface IEnumerator {

  • bject Current { get; }

bool MoveNext(); void Reset() => throw new InvalidOperationException("Reset not supported"); } public interface IEnumerator<out T> : IDisposable, IEnumerator { new T Current { get; }

  • bject IEnumerator.Current => Current;

}

slide-21
SLIDE 21

C# C# 8.0, 8.0, … … – long term thinking

Extension Everything Async Streams and Disposables More patterns: recursive, tuples, expressions, etc Records Discriminated Unions Creating Immutable objects

slide-22
SLIDE 22

Extension everything

extension Enrollee extends Person { // static field static Dictionary<Person, Professor> enrollees = new(); // instance method public void Enroll(Professor supervisor) { enrollees[this] = supervisor; } // instance property public Professor Supervisor => enrollees.TryGetValue(this, out var supervisor) ? supervisor : null; // static property public static ICollection<Person> Students => enrollees.Keys; // instance constructor public Person(string name, Professor supervisor) : this(name) { this.Enroll(supervisor); } }

slide-23
SLIDE 23

Async streams and disposables

IAsyncEnumerable<Person> people = database.GetPeopleAsync(); foreach await (var person in people) { // use person } using await (IAsyncDisposable resource = await store.GetRecordAsync(…)) { // use resource }

slide-24
SLIDE 24

Recursive patterns

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

slide-25
SLIDE 25

Pattern matching expressions

var s = match (o) { int i => $"Number {i}", case Point(int x, int y) => $"({x},{y})", string s when s.Length > 0 => s, null => "<null>", _ => "<other>" };

slide-26
SLIDE 26

Tuples in patterns

state = match (state, request) { (Closed, Open) => Opened, (Closed, Lock) => Locked, (Opened, Close) => Closed, (Locked, Unlock) => Closed, _ => throw new InvalidOperationException(…) }

slide-27
SLIDE 27

Records and discriminated unions

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 void Deconstruct(out string first, out string last){ first = First; last = Last; } public bool Equals(Person other) => other != null && 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); … }

record Person(string First, string Last);

slide-28
SLIDE 28

Creating immutable objects

var p1 = new Point { X = 3, Y = 7 }; var p2 = p1 with { X = -p1.X };

slide-29
SLIDE 29

Questions and resources?

Language https://github.com/dotnet/csharplang Implementation https://github.com/dotnet/roslyn Reference http://source.roslyn.io Contact https://twitter.com/roslyn Content https://github.com/Pilchie/QConSP/ Me kevinpi@microsoft.com @Pilchie – Twitter/GitHub