Answers for "c# switch case enum"

C#
1

enum switch menu c#

enum Menu
        {
            Students_Detail = 1,
            Display = 2,
            Exit = 3
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Please select a menu option");
            Console.WriteLine("1. Students Detail");
            Console.WriteLine("2. Display");
            Console.WriteLine("3. Exit");
            int pick = Convert.ToInt32(Console.ReadLine());
            Menu menuChoice = new Menu();
            menuChoice = (Menu)pick;

            switch (pick)
            {
                case 1:
                    break;
                case 2:
                    break;
                case 3:
                    break;
                default:
                    break;
            }
        }
Posted by: Guest on December-06-2020
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
2

c# switch case

public class Example
{
  // Button click event
  public void Click(object sender, RoutedEventArgs e)
  {
            if (sender is Button handler)
            {
                switch (handler.Tag.ToString())
                {
                  case string tag when tag.StartsWith("Example"):
                       // your code
                    break;
                    
                  default:
                    break;
                }
            }
  }
}
Posted by: Guest on August-27-2020
-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

C# Answers by Framework

Browse Popular Code Answers by Language