Answers for "switch case with enum"

1

C# Switch case enum

enum Country { Spain, USA, Japan };
static void Main(string[] args)
{
    Country country;
    Console.WriteLine("Enter the number of country\n 1.Spain \n 2.The USA \n 3.Japan");
    string input = Console.ReadLine();
    bool sucess = Enum.TryParse<Country>(input, out country);

    if (!sucess)
    {
        Console.WriteLine("entry {0} is not a valid country", input);
        return;
    }

    switch (country)
    {
        case Country.Spain:
            Console.WriteLine("Its in Europe");
            break;
        case Country.USA:
            Console.WriteLine("Its in North America");
            break;
        case Country.Japan:
            Console.WriteLine("Its in Asia");
            break;
    }
    Console.ReadKey();
}

public static RGBColor FromRainbow(Rainbow colorBand) =>
    colorBand switch
    {
        Rainbow.Red    => new RGBColor(0xFF, 0x00, 0x00),
        Rainbow.Orange => new RGBColor(0xFF, 0x7F, 0x00),
        Rainbow.Yellow => new RGBColor(0xFF, 0xFF, 0x00),
        Rainbow.Green  => new RGBColor(0x00, 0xFF, 0x00),
        Rainbow.Blue   => new RGBColor(0x00, 0x00, 0xFF),
        Rainbow.Indigo => new RGBColor(0x4B, 0x00, 0x82),
        Rainbow.Violet => new RGBColor(0x94, 0x00, 0xD3),
        _              => throw new ArgumentException(message: "invalid enum value", paramName: nameof(colorBand)),
    };


public static RGBColor FromRainbowClassic(Rainbow colorBand)
{
    switch (colorBand)
    {
        case Rainbow.Red:
            return new RGBColor(0xFF, 0x00, 0x00);
        case Rainbow.Orange:
            return new RGBColor(0xFF, 0x7F, 0x00);
        case Rainbow.Yellow:
            return new RGBColor(0xFF, 0xFF, 0x00);
        case Rainbow.Green:
            return new RGBColor(0x00, 0xFF, 0x00);
        case Rainbow.Blue:
            return new RGBColor(0x00, 0x00, 0xFF);
        case Rainbow.Indigo:
            return new RGBColor(0x4B, 0x00, 0x82);
        case Rainbow.Violet:
            return new RGBColor(0x94, 0x00, 0xD3);
        default:
            throw new ArgumentException(message: "invalid enum value", paramName: nameof(colorBand));
    };
}
Posted by: Guest on August-07-2021
-1

c# switch case enum

public enum Operator
{
    PLUS, MINUS, MULTIPLY, DIVIDE
}


switch(op)
{
     case Operator.PLUS:
     {
        // your code 
        // for plus operator
        break;
     }
     case Operator.MULTIPLY:
     {
        // your code 
        // for MULTIPLY operator
        break;
     }
     default: break;
}
Posted by: Guest on December-22-2020

Code answers related to "switch case with enum"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language