c# 语法糖 switch表达式

c# 语法糖 switch表达式_第1张图片

c# 语法糖 switch表达式_第2张图片

源代码

``` using System.Drawing;

namespace 语法糖 { internal class Program { static void Main(string[] args) {

}

    private Color GetColorRgb(ColorEnum colorEnum)
    {
        switch(colorEnum)
        {
            case ColorEnum.Red:
                return Color.FromArgb(0, 0, 0);
            case ColorEnum.Blue:
                return Color.FromArgb(0, 0, 0);
            case ColorEnum.Black:
                return Color.FromArgb(0, 0, 0);
            default:
                throw new ArgumentException(
                    message: "invalid enum value",
                    paramName: nameof(colorEnum)
                    );
        }
    }

    private Color GetColorRgb2(ColorEnum colorEnum)
    {
        return colorEnum switch
        {
            ColorEnum.Red => Color.FromArgb(0, 0, 0),
            ColorEnum.Blue => Color.FromArgb(0, 0, 0),
            ColorEnum.Black => Color.FromArgb(0, 0, 0),
            _ => throw new ArgumentException(
                    message: "invalid enum value",
                    paramName: nameof(colorEnum)
                    )
        };
    }

    public enum ColorEnum
    {
        Red,
        Black,
        Blue,
    }

}

} ```

你可能感兴趣的:(c#,java,前端,javascript,数据库)