当前位置: 首页>>代码示例>>C#>>正文


C# Enum.IsDefined方法代码示例

本文整理汇总了C#中System.Enum.IsDefined方法的典型用法代码示例。如果您正苦于以下问题:C# Enum.IsDefined方法的具体用法?C# Enum.IsDefined怎么用?C# Enum.IsDefined使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Enum的用法示例。


在下文中一共展示了Enum.IsDefined方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

//引入命名空间
using System;

[Flags] public enum PetType
{
   None = 0, Dog = 1, Cat = 2, Rodent = 4, Bird = 8, Reptile = 16, Other = 32
};

public class Example
{
   public static void Main()
   {
      object value; 
      
      // Call IsDefined with underlying integral value of member.
      value = 1;
      Console.WriteLine("{0}: {1}", value, Enum.IsDefined(typeof(PetType), value));
      // Call IsDefined with invalid underlying integral value.
      value = 64;
      Console.WriteLine("{0}: {1}", value, Enum.IsDefined(typeof(PetType), value));
      // Call IsDefined with string containing member name.
      value = "Rodent";
      Console.WriteLine("{0}: {1}", value, Enum.IsDefined(typeof(PetType), value));
      // Call IsDefined with a variable of type PetType.
      value = PetType.Dog;
      Console.WriteLine("{0}: {1}", value, Enum.IsDefined(typeof(PetType), value));
      value = PetType.Dog | PetType.Cat;
      Console.WriteLine("{0}: {1}", value, Enum.IsDefined(typeof(PetType), value));
      // Call IsDefined with uppercase member name.      
      value = "None";
      Console.WriteLine("{0}: {1}", value, Enum.IsDefined(typeof(PetType), value));
      value = "NONE";
      Console.WriteLine("{0}: {1}", value, Enum.IsDefined(typeof(PetType), value));
      // Call IsDefined with combined value
      value = PetType.Dog | PetType.Bird;
      Console.WriteLine("{0:D}: {1}", value, Enum.IsDefined(typeof(PetType), value));
      value = value.ToString();
      Console.WriteLine("{0:D}: {1}", value, Enum.IsDefined(typeof(PetType), value));
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:40,代码来源:Enum.IsDefined

输出:

1: True
64: False
Rodent: True
Dog: True
Dog, Cat: False
None: True
NONE: False
9: False
Dog, Bird: False

示例2: Main

//引入命名空间
using System;

[Flags] public enum Pets { 
      None = 0, Dog = 1, Cat = 2, Bird = 4, 
      Rodent = 8, Other = 16 };

public class Example
{
   public static void Main()
   {
      Pets value = Pets.Dog | Pets.Cat;
      Console.WriteLine("{0:D} Exists: {1}", 
                        value, Pets.IsDefined(typeof(Pets), value));
      string name = value.ToString();
      Console.WriteLine("{0} Exists: {1}", 
                        name, Pets.IsDefined(typeof(Pets), name));
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:19,代码来源:Enum.IsDefined

输出:

3 Exists: False
Dog, Cat Exists: False

示例3: Main

//引入命名空间
using System;

enum Color
{
    red,
    green,
    yellow
}

public class MainClass
{
    public static void Main()
    {

        
        
        bool defined = Enum.IsDefined(typeof(Color), 5);
        Console.WriteLine("5 is a defined value for Color: {0}", defined);    
        
    }
}
开发者ID:C#程序员,项目名称:System,代码行数:22,代码来源:Enum.IsDefined


注:本文中的System.Enum.IsDefined方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。