C# 7.0 i co dalej...

O mnie

  • CxO@Octal Solutions
  • Microsoft MVP
  • lider Wroc.NET
  • dotnetomaniak.pl
  • http://blog.octal.pl
  • @pawel_lukasik
  • devWarsztaty@Wrocław
  • OstraPiła
  • dotnetconf.pl

Historia

  • C# 1.0
  • C# 2.0 - 6.0

Agenda

  • C# 7.0
  • C# 7.1, 7.2, ...
  • C# 8, 9

C# 7.0

  • pattern matching
  • out
  • literały
  • funkcje lokalne
  • Tuple
  • Ref retutrns & ref locals
  • Expression bodied functions
  • Throw expressions

Demo

Literały

int binary = 0b111000;
int bcd = 0b1110_0001;
//....
int value = 123_1_2_11;

Parametry out

if (int.TryParse("1977", out int year))
{
    Console.WriteLine($"Ok! year: {year}");
}
if (int.TryParse("1977", out _))
{
    Console.WriteLine("Ok. But now year {_}");
}

Pattern matching

switch (character)
{
    case DarthVader dv:
        dv.Noooooooo();
        break;
    case PrincessLeia pl:
        pl.Say();
        break;
    case BobaFett b when b.Episode == 5:
        b.HuntHanSolo();
        break;
    case BobaFett b:
        b.DoNothing();
        break;
    case R2D2 r2d2:
        r2d2.Beep();
        break;
}

Tuple

var t = (a: "ala", ma: "kota");
Console.WriteLine(t.a);
Console.WriteLine(t.ma);
var (a, ma) = t;
Console.WriteLine(a);
Console.WriteLine(ma);
var user = (firstName: "Paweł", lastName: "Łukasik");
var (firstname, _) = user;

Funkcje lokalne

string LocalFunction()
{
    string LocalFunction2() => "function";
    return "Local" + LocalFunction2();
}

Console.WriteLine(LocalFunction());

Ref returns & ref locals

public static ref Point GetHalfPoint()
{
    return ref _p;
}

//....
ref var p1 = ref GetHalfPoint();

Expression Bodied Functions

public string Name
{
    get => _name;
    set => _name = value;
}

ExpressionBodiedFunctionDemo(string name) => Name = name;

~ExpressionBodiedFunctionDemo() => Name = null;

Throw Expressions

public void NewWay(string param1, int? param2)
{
    P1 = param1 ?? throw new ArgumentNullException(nameof(param1));
    P2 = param2 ?? throw new ArgumentNullException(nameof(param2));
}

C# 7.1

  • async Main
  • (more) Tuples
  • defaults

C# 7.2

  • reference semantics
  • non-trailing named args
  • (more) literals
  • private protected

Demo

C# 8.0

Preview

Nullable reference types

class Person
{
    public string FirstName;   // Not null
    public string? MiddleName; // May be null
    public string LastName;    // Not null
}

https://blogs.msdn.microsoft.com/dotnet/2017/11/15/nullable-reference-types-in-csharp/

async streams

foreach await (string s in asyncStream) 
{
  //...
}

async dispose

using await (var transaction = connection.BeginTransaction())
{
    // do important work

    transaction.Commit();
}

Record types

class Person(string First, string Last);

https://www.erikheemskerk.nl/c-sharp-7-2-and-8-0-uncertainty-awesomeness/

Extension everything

extension CustomerExt extends Customer {
    //methods and properties go here
}

Default interface impl

public interface ILogger
{
    void Log(LogLevel level, string message);

    void Log(LogLevel level, string format, params object[] arguments)
    {
        Log(level, string.Format(format, arguments));
    }

    void Debug(string message)
    {
        Log(LogLevel.Debug, message);
    }

    void Debug(string format, params object[] arguments)
    {
        Log(LogLevel.Debug, string.Format(format, arguments));
    }
}

https://www.erikheemskerk.nl/c-sharp-7-2-and-8-0-uncertainty-awesomeness/

C# 9.0

;)

Dodatkowe materiały

Pytania?

Uwagi?

Dziękuję za uwagę!

@pawel_lukasik

C# 7.0 i co dalej...

By Pawel Lukasik

C# 7.0 i co dalej...

  • 1,442
Loading comments...

More from Pawel Lukasik