Ajouter un commentaire

.NET Core 3 preview 2

Par:
fredericmazue

jeu, 31/01/2019 - 17:18

Microsoft a annoncé la disponibilité de la deuxième préversion de .NET Core 3 : .NET Core 3 preview 2.

On y remarque un sucre syntaxique :-)  à l'attention des paresseux (en informatique la paresse est souvent une vertu ;-) Si vous êtes fatigué d'utiliser des instructions qui nécessitent d'indenter votre code, vous pouvez maintenant écrire le code suivant, qui attache une déclaration using à la portée du bloc d'instructions actuel, puis dispose de l'objet à la fin de celui-ci.

static void Main(string[] args)
{
    using var options = Parse(args);

    if (options["verbose"]) { WriteLine("Logging..."); }
} // options disposed here

C#8 enrichit les correspondances de motifs (pattern matching) de C#7 aux motifs récursifs et apporte l'expression switch. Avec elle, switch devient un opérateur infoxe, pour une syntaxe simplifiée et plus claire. Par exemple :

static string Display(object o) => o switch
{
    Point { X: 0, Y: 0 }         => "origin",
    Point { X: var x, Y: var y } => $"({x}, {y})",
    _                            => "unknown"
};

Autre exemple, avec tests de postions des paramètres et déconstructions de tuples :

static State ChangeState(State current, Transition transition, bool hasKey) =>
    (current, transition) switch
    {
        (Opened, Close)              => Closed,
        (Closed, Open)               => Opened,
        (Closed, Lock)   when hasKey => Locked,
        (Locked, Unlock) when hasKey => Closed,
        _ => throw new InvalidOperationException($"Invalid transition")
    };

A remarquer encore, l'outil de flux Utf8JsonWriter qui permet décrire des types .NET dans du texdte JSON encodé en UTF-8. Par exemple :

static int WriteJson(IBufferWriter<byte> output, long[] extraData)
{
    var json = new Utf8JsonWriter(output, state: default);
    json.WriteStartObject();
    json.WriteNumber("age", 15, escape: false);
    json.WriteString("date", DateTime.Now);
    json.WriteString("first", "John");
    json.WriteString("last", "Smith");
    json.WriteStartArray("phoneNumbers", escape: false);
    json.WriteStringValue("425-000-1212", escape: false);
    json.WriteStringValue("425-000-1213");
    json.WriteEndArray();
    json.WriteStartObject("address");
    json.WriteString("street", "1 Microsoft Way");
    json.WriteString("city", "Redmond");
    json.WriteNumber("zip", 98052);
    json.WriteEndObject();
    json.WriteStartArray("ExtraArray");
    for (var i = 0; i < extraData.Length; i++)
    {
        json.WriteNumberValue(extraData[i]);
    }
    json.WriteEndArray();
    json.WriteEndObject();
    json.Flush(isFinalBlock: true);

    return (int)json.BytesWritten;
}

.NET Core 3 preview 2 peut être téléchargé ici. https://dotnet.microsoft.com/download/dotnet-core/3.0

Filtered HTML

Plain text

CAPTCHA
Cette question permet de vérifier que vous n'êtes pas un robot spammeur :-)
  AA    GGG    CCC   AA   ZZZZZ 
A A G C A A Z
AAAA G GG C AAAA Z
A A G G C A A Z
A A GGG CCC A A ZZZZZ