英文:
Use an Enum in a Switch Case C#
问题
我有一个不同AppTypes的枚举。
public enum AppType
{
DefaultApps = 1,
GlobalApps = 3,
PrivateApps = 'L'
}
我将收到一个形如"1"、"3"或"L"的字符串,并希望在switch语句中使用它。
我尝试像下面这样做,但不起作用。我漏掉了什么吗?
switch (appTypeString)
{
case $"{(char)AppType.DefaultApps}":
Console.WriteLine("Default Catalog Apps");
break;
case nameof(AppType.GlobalApps):
Console.WriteLine("Global Catalog Apps");
break;
case nameof(AppType.PrivateApps):
Console.WriteLine("Private Catalog Apps");
break;
default:
Console.WriteLine("Unknown App Type");
break;
}
英文:
I have an Enum of different AppTypes.
public enum AppType
{
DefaultApps = 1,
GlobalApps = 3,
PrivateApps = 'L'
}
I will receive a string of form "1", "3" or "L" and want to use this in switch statement.
I am trying to do as below. but not working. am I missing anything.
switch (appTypeString)
{
case $"{(char)AppType.DefaultApps}:": // also tried nameof(AppType.GlobalApps)
Console.WriteLine("Default Catalog Apps");
break;
case nameof(AppType.GlobalApps):
Console.WriteLine("Global Catalog Apps");
break;
case nameof(AppType.PrivateApps):
Console.WriteLine("Private Catalog Apps");
break;
default:
Console.WriteLine("Unknown App Type");
break;
}
答案1
得分: 2
以下是翻译好的代码部分:
你可以这样做:
AppType test = AppType.DefaultApps;
switch (test)
{
case AppType.DefaultApps:
Console.WriteLine("默认目录应用");
break;
case AppType.GlobalApps:
Console.WriteLine("全局目录应用");
break;
case AppType.PrivateApps:
Console.WriteLine("私有目录应用");
break;
default:
Console.WriteLine("未知应用类型");
break;
}
在你的情况下,你的输入是一个字符串,我会这样做:
```csharp
string val = "L";
AppType test;
bool flag = Enum.TryParse(val, out test);
// 由于AppType.PrivateApps的类型是char,它将无法通过Enum.TryParse(),因此我们进行转换
if (!flag)
{
if (val.Length == 1)
test = (AppType)(val.ToCharArray().First());
}
switch (test)
{
case AppType.DefaultApps:
Console.WriteLine("默认目录应用");
break;
case AppType.GlobalApps:
Console.WriteLine("全局目录应用");
break;
case AppType.PrivateApps:
Console.WriteLine("私有目录应用");
break;
default:
Console.WriteLine("未知应用类型");
break;
}
<details>
<summary>英文:</summary>
You can do like this:
AppType test = AppType.DefaultApps;
switch (test)
{
case AppType.DefaultApps:
Console.WriteLine("Default Catalog Apps");
break;
case AppType.GlobalApps:
Console.WriteLine("Global Catalog Apps");
break;
case AppType.PrivateApps:
Console.WriteLine("Private Catalog Apps");
break;
default:
Console.WriteLine("Unknown App Type");
break;
}
In your case you have a string as the Input, I would do like this:
string val = "L";
AppType test;
bool flag = Enum.TryParse(val, out test);
//Since AppType.PrivateApps is of type char it will fail the Enum.TryParse() so we make a conversion
if (!flag)
{
if(val.Length == 1)
test = (AppType)(val.ToCharArray().First());
}
switch (test)
{
case AppType.DefaultApps:
Console.WriteLine("Default Catalog Apps");
break;
case AppType.GlobalApps:
Console.WriteLine("Global Catalog Apps");
break;
case AppType.PrivateApps:
Console.WriteLine("Private Catalog Apps");
break;
default:
Console.WriteLine("Unknown App Type");
break;
}
</details>
# 答案2
**得分**: 0
你还可以使用新的 `switch` 表达式:
```cs
var toPrint = appTypeString switch
{
AppType.DefaultApps => "默认目录应用",
AppType.GlobalApps => "全局目录应用",
AppType.PrivateApps => "私有目录应用",
_ => "未知应用类型",
};
Console.WriteLine(toPrint);
英文:
You can also use the new switch
expression:
var toPrint = appTypeString switch
{
AppType.DefaultApps => "Default Catalog Apps",
AppType.GlobalApps => "Global Catalog Apps",
AppType.PrivateApps => "Private Catalog Apps",
_ => "Unknown App Type",
};
Console.WriteLine(toPrint);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论