【问题标题】:Is it possible to search an enum types list of values to see if an int exists?是否可以搜索枚举类型的值列表以查看是否存在 int?
【发布时间】:2011-03-30 01:01:55
【问题描述】:

说我有一个枚举

public myEnum
{
   value1, value2

}

如果我有一个整数,我可以查看它是否存在于 myEnum 中吗?

【问题讨论】:

  • 为什么?这不是枚举的用途。使用列表或类似的东西和 Linq。
  • 有时我希望我们能对 cme​​ts 投反对票。
  • 枚举是自动生成的数据层的一部分,不能轻易改变它。它是 db 引用表中的值列表

标签: c# .net c#-4.0 enums


【解决方案1】:

使用Enum.IsDefined()。完整的定义是

public static bool IsDefined(
    Type enumType,
    Object value
)

示例用法:

public enum MyEnum { A = 1, B = 2 };

Enum.IsDefined(typeof(MyEnum), 1) --> true
Enum.IsDefined(typeof(MyEnum), 3) --> false

您可以找到更多信息on this msdn page

【讨论】:

    猜你喜欢
    • 2019-01-24
    • 2022-11-10
    • 1970-01-01
    • 2012-06-16
    • 2012-05-30
    • 1970-01-01
    相关资源
    最近更新 更多