quot;Use of unassigned local variablequot; compiler error for switch statement in C#?(“使用未分配的局部变量C#中switch语句的编译器错误?)
问题描述
我有以下 C# 代码:
I have the following C# code:
AnimalTypeEnum animal;
string s = Console.ReadLine();
switch (s.ToLower())
{
case "dog":
animal = AnimalTypeEnum.DOG;
break;
case "cat":
animal = AnimalTypeEnum.CAT;
break;
case "rabbit":
animal = AnimalTypeEnum.RABBIT;
break;
}
Console.WriteLine(animal); #compiler error here
我在最后一行收到此错误:Use of unassigned local variable 'animal'
.我知道这是因为 animal
可能没有取决于用户输入的值,那么我该如何解决呢?
I get this error on the last line: Use of unassigned local variable 'animal'
. I know that it's because animal
may not have a value depending on the user input, so how do I fix that?
理想情况下,如果输入了未知的动物类型,我希望显示一条错误消息,并让用户再次输入该值.
Ideally I'd like to show an error message if an unknown animal type was entered and make the user input the value again.
谢谢.
推荐答案
这里有一种解决方法,使用递归调用而不是需要捕获和抛出异常,或者使用循环(这种情况下的循环会混淆含义在我看来;太多关于你如何做而不是你在做什么):
Here's one way to fix it, using recursive calls instead of needing to catch and throw exceptions, or use a loop (loops in a case like this obfuscate the meaning in my opinion; too much about how you're doing it instead of what you're doing):
private static AnimalTypeEnum GetAnimalFromInput()
{
AnimalTypeEnum animal;
string s = Console.ReadLine();
switch (s.ToLower())
{
case "dog":
animal = AnimalTypeEnum.DOG;
break;
case "cat":
animal = AnimalTypeEnum.CAT;
break;
case "rabbit":
animal = AnimalTypeEnum.RABBIT;
break;
default:
Console.WriteLine(s + " is not valid, please try again");
animal = GetAnimalFromInput();
break;
}
return animal;
}
static void Main(string[] args)
{
AnimalTypeEnum animal = GetAnimalFromInput();
Console.WriteLine(animal);
}
我还要注意,使用 if (s.Equals("dog", StringComparison.CurrentCultureIgnoreCase))
(或适当的不区分大小写的比较)以使其在其他文化中有效.当然,这可能不适用于您的场景(例如测试/家庭作业应用程序,或仅可能在您的文化中使用的东西).
I'll also note that it's good practice to refactor your switch into an if/else chain, using if (s.Equals("dog", StringComparison.CurrentCultureIgnoreCase))
(or the appropriate case-insensitive comparison) to keep it working in other cultures. Of course, this may not apply to your scenario (e.g. test/homework app, or something that will only possibly be used in your culture).
更新:感谢 Mennan Kara 的想法,如果您的值(例如 "dog"
)将始终与枚举值匹配(例如 DOG
),那么你可以使用 Enum.TryParse
来改进你的代码:
Update: Thanks to Mennan Kara for the idea, if your values (e.g. "dog"
) will always match the enum's values (e.g. DOG
), then you can use Enum.TryParse
to improve your code:
private static AnimalTypeEnum GetAnimalFromInput()
{
AnimalTypeEnum animal;
string s = Console.ReadLine();
if (Enum.TryParse(s, true, out animal))
return animal;
else
{
Console.WriteLine(s + " is not valid, please try again");
return GetAnimalFromInput();
}
}
如果您需要灵活地将它们分开,请保留现有的开关.
If you need the flexibility of having them separate, then keep your existing switch.
这篇关于“使用未分配的局部变量"C#中switch语句的编译器错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:“使用未分配的局部变量"C#中switch语句的编译器错误?
基础教程推荐
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- c# Math.Sqrt 实现 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01