在C#中使用枚举在Switch Case中:

huangapple go评论80阅读模式
英文:

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 =&gt; &quot;Default Catalog Apps&quot;,
      AppType.GlobalApps =&gt; &quot;Global Catalog Apps&quot;,
      AppType.PrivateApps =&gt; &quot;Private Catalog Apps&quot;,
      _ =&gt; &quot;Unknown App Type&quot;,
  };

Console.WriteLine(toPrint);

huangapple
  • 本文由 发表于 2023年6月12日 23:45:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76458277.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定