Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【C# 9.0 がやってきた】Build 2020 で .NET 5 Preview 4 公開 #7

Closed
ufcpp opened this issue May 20, 2020 · 2 comments
Closed

Comments

@ufcpp
Copy link
Collaborator

ufcpp commented May 20, 2020

Microsoft Build に合わせていろいろとブログ公開が。

追記: C# チームも1日遅れでブログ出した

正直、ブログ読みふけってて Build のキーノート(ブチザッキ参照)全然聞いてない。

1回の配信で触れる量じゃないし、とりあえずC# 9.0 の話を中心に(他はチャットの反応次第)。

C# 9.0:

@ufcpp-live
Copy link
Owner

配信中に書き散らかした結果:

// new features in .NET 5 preview 4

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Runtime.InteropServices.ComTypes;
using System.Threading;

interface IA { int A(); }
interface IB : IA { int B(); }
interface IC : IA { int C(); }

class Ab : IB, IC
{
    public int A() => 2;
    public int B() => 3;
    public int C() => 5;
}

class Program
{
    static void Main()
    {
        Console.WriteLine(float.NaN == float.NaN); //false

        //// native int
        //nint i = 1;
        //nint u = 2;
        //Console.WriteLine(i + u);
    }

    // target-typed new
    static Dictionary<string, int> d = new()
    {
        { "a", 1 },
    };

    static void M1(object obj)
    {
        // !ReferenceEquals(obj, null)
        if (obj is { }) // 知ってないと意味不明
        {
        }
        // 同じ意味
        if (obj is not null) // 初見さんにやさしい
        {
        }

        // ユーザー定義の != があればそれを呼ぶ
        if (obj != null)
        {
        }

        IA a = obj is null
            ? (IA)null
            : (IB)null;

        int b1 = obj is null
            ? (byte)1
            : (int)2;

        int? c1 = obj is null
            ? (int?)null
            : (int)2;

        IA aa = obj switch
        {
            IB b => b,
            IC c => c,
        };

        M((short)1, obj is null ? (int)1 : (short)2);
    }

    static void M(short x, short y) { }
    static void M(int x, int y) { }

    static void M<T>(T obj)
        where T : IB, IC // and
    {
        obj.A();
        obj.B();
        obj.C();
    }

    //static void M<T>(T obj)
    //    where T : IB or IC // 要望はあり。別提案
    //{
    //    obj.A();
    //    obj.B();
    //    obj.C();
    //}

    static int M11(object obj) => obj switch
    {
        byte => 1,
        sbyte => 1,
        ushort => 2,
        short => 2,
        int => 4,
        uint => 4,
        long => 8,
        ulong => 8,
        DateTime => 8,
        DateTimeOffset => 16,
        Guid => 16,
        _ => -1,
    };

    static int M11(bool b) => b switch
    {
        true => 1,
        false => 0,
    };

    class A { }
    class B : A { }
    class C : A { }

#nullable enable
    // pattern V3 (and, or, not, simple type, parenthesized...)
    static int M(object? obj) => obj switch
    {
        IB b and IC c => b.A() * b.B() * c.C(),
        (IA or IB) and var a => a.A(),
        //(B or C) and var a => a.A(), // object です(今のところ)。A にする提案あり
        string s => s.Length,
        //{ } a => a.GetHashCode(), // not null の意味含む(初見に厳しい)
        not null and var a => a.GetHashCode(),
    };

    // pattern V3 (rerational, exhaustivity check)
    static int M(float value) => value switch
    {
        < (10 * 10) => 1,
        >= 100 and < 500 => 2,
        5 * 100 => 5,
        > 500 => 3,
        float.NaN => 4,
        //_ => 2,
    };

    static int M111(float value) => value switch
    {
        var x when x < 100 => 1,
        var x when x >= 100 && x <= 500 => 2,
        var x when x > 500 => 3,
    };

    // pattern V3 (byte exhaustivity check)
    static int M(byte ascii) => ascii switch
    {
        <= 31 => 0, // control
        32 => 2, // white space
        <= 47 => 3, // symbol
        <= 57 => 4, // number
        <= 64 => 3, // symbol
        >= (int)'A' and <= (int)'Z' => 5, // letter A-Z
        >= (int)'a' and <= (int)'z' => 5, // letter a-z
        > (int)'z' and <= 126 => 3,
        127 => 0,
        >= 128 => 6, // non-asci
    };
}

@ufcpp-live
Copy link
Owner

@ufcpp ufcpp mentioned this issue Jun 8, 2020
19 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants