Skip to main content
本文から 8 文字削除
ソース リンク
user39889
user39889

三項演算子(ternary operator)を使う方法も考えられます。

using System;
                    
public class Program
{
    public static void Main()
    {
        test(0);
        test(1);
        test(2);
        test(3);
    }

    static void test(int val) {
        var res = (val >== 01 ? val-10 : val) switch {
            0 => A(), // 0 or 1
            12 => B(), // 2
            _ => throw new Exception()
        };
        Console.WriteLine("{0}: {1}", val, res);
    }

    static string A() { return "A"; }
    static string B() { return "B"; }
}

実行結果

0: A
1: A
2: B
Unhandled exception. System.Exception: Exception of type 'System.Exception' was thrown.
   at Program.test(Int32 val)
   at Program.Main()
Command terminated by signal 6

Playground: C# Online Compiler | .NET FiddleC# Online Compiler | .NET Fiddle

三項演算子(ternary operator)を使う方法も考えられます。

using System;
                    
public class Program
{
    public static void Main()
    {
        test(0);
        test(1);
        test(2);
        test(3);
    }

    static void test(int val) {
        var res = (val > 0 ? val-1 : val) switch {
            0 => A(), // 0 or 1
            1 => B(), // 2
            _ => throw new Exception()
        };
        Console.WriteLine("{0}: {1}", val, res);
    }

    static string A() { return "A"; }
    static string B() { return "B"; }
}

実行結果

0: A
1: A
2: B
Unhandled exception. System.Exception: Exception of type 'System.Exception' was thrown.
   at Program.test(Int32 val)
   at Program.Main()
Command terminated by signal 6

Playground: C# Online Compiler | .NET Fiddle

三項演算子(ternary operator)を使う方法も考えられます。

using System;
                    
public class Program
{
    public static void Main()
    {
        test(0);
        test(1);
        test(2);
        test(3);
    }

    static void test(int val) {
        var res = (val == 1 ? 0 : val) switch {
            0 => A(), // 0 or 1
            2 => B(),
            _ => throw new Exception()
        };
        Console.WriteLine("{0}: {1}", val, res);
    }

    static string A() { return "A"; }
    static string B() { return "B"; }
}

実行結果

0: A
1: A
2: B
Unhandled exception. System.Exception: Exception of type 'System.Exception' was thrown.
   at Program.test(Int32 val)
   at Program.Main()
Command terminated by signal 6

Playground: C# Online Compiler | .NET Fiddle

ソース リンク
user39889
user39889

三項演算子(ternary operator)を使う方法も考えられます。

using System;
                    
public class Program
{
    public static void Main()
    {
        test(0);
        test(1);
        test(2);
        test(3);
    }

    static void test(int val) {
        var res = (val > 0 ? val-1 : val) switch {
            0 => A(), // 0 or 1
            1 => B(), // 2
            _ => throw new Exception()
        };
        Console.WriteLine("{0}: {1}", val, res);
    }

    static string A() { return "A"; }
    static string B() { return "B"; }
}

実行結果

0: A
1: A
2: B
Unhandled exception. System.Exception: Exception of type 'System.Exception' was thrown.
   at Program.test(Int32 val)
   at Program.Main()
Command terminated by signal 6

Playground: C# Online Compiler | .NET Fiddle