In switch statement can we use enum in C# [closed]
Suppose we want to give condition using switch statement with the use of enum. Can we do that? If yes, then how?
Yes, it works fine. The Lesson 17: Enums article offers this example:
// declares the enum
public enum Volume
{
Low,
Medium,
High
}
// demonstrates how to use the enum
class EnumSwitch
{
static void Main()
{
// create and initialize
// instance of enum type
Volume myVolume = Volume.Medium;
// make decision based
// on enum value
switch (myVolume)
{
case Volume.Low:
Console.WriteLine("The volume has been turned Down.");
break;
case Volume.Medium:
Console.WriteLine("The volume is in the middle.");
break;
case Volume.High:
Console.WriteLine("The volume has been turned up.");
break;
}
Console.ReadLine();
}
}
Have a look at
Lesson 17: Enums
Ya,you can use enums in switch statement.
精彩评论