Ejemplo n.º 1
0
    static void Main(string[] args)
    {
        Console.WriteLine("Hello World!");
        S1 s1 = default;
        S2 s2 = default(S2);

        Console.WriteLine(s1.ToString());
        Console.WriteLine(s2.ToString());

        // initializer list works for struct and class
        var dataStruct = new DataStruct()
        {
            x = 1, y = 2, z = 3
        };
        var dataClass = new DataClass()
        {
            x = 1, y = 2, z = 3
        };

        Console.WriteLine(dataStruct.ToString());
        Console.WriteLine(dataClass.ToString());

        // initializer runs last
        //var x = new X<int> { };
        //var x = new X<int>() {};
        //var x = new X<int>() {x = 3};     // overrides constructor
        var x = new X <int>(10)
        {
            x = 3
        };                                    // overrides constructor

        Console.WriteLine(x.ToString());
        Console.WriteLine(x.x.ToString());

        // var c0 = new C;  // () {} [] expected
        var c1 = new C();
        var c3 = new C()
        {
        };
        var c4 = new C {
        };

        bool found = true;

        if (found)
        {
            int j = 1 switch { _ => 0 };
            Console.WriteLine($"found {1 + 1}");
        }

        // tuple
        var vals0 = (1, 2, 3, 4);
        int _     = vals0 switch {
            (1, 1, 1, 1) => 1,
            (2, 2, 2, 2) => 2,
            (3, 3, 3, 3) => 3,
            (4, 4, 4, 4) => 4,
            (5, 5, _, _) => 5,
            (var a, var b, var c, var d)when a + b == c + d => a + b + c + d,
            _ => 0,
        };

        Console.WriteLine($"{_}");

        // don't know if possible
        var vals1 = new int[] { 1, 2, 3, 4 };

        // anonymous object
        var vals2 = new { x = 1, y = 2, z = 3 };

        _ = vals2 switch {
            { x : 1, y : 2 } => 1,