“Use of unassigned local variable” compiler error for switch statement in C#?

前端 未结 4 916
天命终不由人
天命终不由人 2021-01-17 06:11

I have the following C# code:

AnimalTypeEnum animal;
string s = Console.ReadLine();
switch (s.ToLower())
{
case \"dog\":
    animal = AnimalTypeEnum.DOG;
            


        
4条回答
  •  礼貌的吻别
    2021-01-17 07:03

    AnimalTypeEnum animal;
    var s = Console.ReadLine();
    Console.WriteLine(!Enum.TryParse(s, true, out animal) ? "Not a valid animal" : animal.ToString());
    

提交回复
热议问题