gpt4 book ai didi

c# - 如何确定是否为变量分配了枚举中存在的值?

转载 作者:太空宇宙 更新时间:2023-11-03 18:51:56 25 4
gpt4 key购买 nike

我有这个枚举:

namespace J.Enums
{
public enum TH
{
Light = 0,
Dark = 1
}

public static partial class Extensions
{
public static string Text(this TH theme)
{
switch (theme)
{
default:
case TH.Light: return "Light";
case TH.Dark: return "Dark";
}
}

public static TH ToTheme(this string theme)
{
switch (theme)
{
default:
case "Light": return TH.Light;
case "Dark": return TH.Dark;
}
}
}
}

如果我有一个变量 a 如下:

var a = 88;

如何确定 a 的值是否为枚举的有效值?在这种情况下,它不会。

最佳答案

var isDefined = Enum.IsDefined(typeof(TH), a);

关于c# - 如何确定是否为变量分配了枚举中存在的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55850395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com