Enum 成员方法(MSDN)

Enum 成员方法(MSDN)

Posted on 2009-03-03 21:23 ottox 阅读(107) 评论(0) 编辑 收藏
 

Enum 成员方法

名称

说明

CompareTo

将此实例与指定对象进行比较并返回一个对二者的相对值的指示。

Equals

已重载。 已重写。 返回一个值,该值指示此实例是否与指定的对象相等。

Format

根据指定格式将指定枚举类型的指定值转换为其等效的字符串表示形式。

GetHashCode

已重写。 返回该实例的值的哈希代码。

GetName

在指定枚举中检索具有指定值的常数的名称。

GetNames

检索指定枚举中常数名称的数组。

GetType 

获取当前实例的 Type。 (从 Object 继承。)

GetTypeCode

返回此实例的基础 TypeCode。

GetUnderlyingType

返回指定枚举的基础类型。

GetValues

检索指定枚举中常数值的数组。

IsDefined

返回指定枚举中是否存在具有指定值的常数的指示。

Parse

已重载。 将一个或多个枚举常数的名称或数字值的字符串表示转换成等效的枚举对象。

ReferenceEquals 

确定指定的 Object 实例是否是相同的实例。 (从 Object 继承。)

ToObject

已重载。 返回设置为指定值的、指定枚举类型的实例。

ToString

已重载。 已重写。 将此实例的值转换为其等效的字符串表示。

一、CompareTo

将此实例与指定对象进行比较并返回一个对二者的相对值的指示(大小)。

语法

public int CompareTo (Object target)

参数

target

要比较的对象,或为空引用(在 Visual Basic 中为 Nothing)。

返回值

有符号数字,指示此实例和 target 的相对值。

返回值

说明

小于零

此实例的值小于 target 的值。

此实例的值等于 target 的值。

大于零

此实例的值大于 target 的值。

- 或 -

target 为空引用(在 Visual Basic 中为 Nothing)。

示例

using System;

 

public class CompareToTest {

    enum VehicleDoors { Motorbike = 0, Sportscar = 2, Sedan = 4, Hatchback = 5 };

 

    public static void Main() {

        VehicleDoors myVeh = VehicleDoors.Sportscar;

        VehicleDoors yourVeh = VehicleDoors.Motorbike;

        VehicleDoors otherVeh = VehicleDoors.Sedan;

 

        Console.WriteLine("Does a {0} have more doors than a {1}?", myVeh, yourVeh);

        Console.WriteLine( "{0}{1}", myVeh.CompareTo(yourVeh) > 0 ? "Yes" : "No", Environment.NewLine );

 

        Console.WriteLine("Does a {0} have more doors than a {1}?", myVeh, otherVeh);

        Console.WriteLine( "{0}", myVeh.CompareTo(otherVeh) > 0 ? "Yes" : "No" );

    }

}

二、                  Equals

  返回一个值,该值指示此实例是否与指定的对象相等。

重载列表

名称

说明

Enum.Equals (Object)

返回一个值,该值指示此实例是否与指定的对象相等。

.NET Compact Framework 支持。

Enum.Equals (Object, Object)

确定指定的 Object 实例是否被视为相等。

Enum.Equals (Object) :返回一个值,该值指示此实例是否与指定的对象相等。

语法

public override bool Equals (Object obj)

参数

obj 代表与此实例进行比较的 object,或空引用。

返回值

如果 obj 是具有与此实例相同的基础类型和值的 Enum,则为 true;否则为 false。

示例

using System;

public class EqualsTest {

    enum Colors { Red, Green, Blue, Yellow };

    enum Mammals { Cat, Dog, Horse, Dolphin };

 

    public static void Main() {

        Mammals myPet = Mammals.Cat;

        Colors myColor = Colors.Red;

        Mammals yourPet = Mammals.Dog;

        Colors yourColor = Colors.Red;

 

        Console.WriteLine("My favorite animal is a {0}", myPet);

        Console.WriteLine("Your favorite animal is a {0}", yourPet);

        Console.WriteLine("Do we like the same animal? {0}", myPet.Equals(yourPet) ? "Yes" : "No");

 

        Console.WriteLine();

        Console.WriteLine("My favorite color is {0}", myColor);

        Console.WriteLine("Your favorite color is {0}", yourColor);

        Console.WriteLine("Do we like the same color? {0}", myColor.Equals(yourColor) ? "Yes" : "No");

 

        Console.WriteLine();

        Console.WriteLine("The value of my color ({0}) is {1}", myColor, Enum.Format(typeof(Colors), myColor, "d"));

        Console.WriteLine("The value of my pet (a {0}) is {1}", myPet, Enum.Format(typeof(Mammals), myPet, "d"));

        Console.WriteLine("Even though they have the same value, are they equal? {0}",

                    myColor.Equals(myPet) ? "Yes" : "No");

    }

}

4、   Enum.Equals (Object, Object)  确定指定的 Object 实例是否被视为相等。

语法

public static bool Equals (Object objA,Object objB)

参数

objA

要比较的第一个 Object。

objB

要比较的第二个 Object。

返回值

如果 objA 是与 objB 相同的实例,或者如果两者均为空引用,或者如果 objA.Equals(objB) 返回 true,则为 true;否则为 false。

示例

using System;

 

public class MyClass {

   public static void Main() {

   string s1 = "Tom";

   string s2 = "Carol";

   Console.WriteLine("Object.Equals(""{0}"", ""{1}"") => {2}",

      s1, s2, Object.Equals(s1, s2));

 

   s1 = "Tom";

   s2 = "Tom";

   Console.WriteLine("Object.Equals(""{0}"", ""{1}"") => {2}",

      s1, s2, Object.Equals(s1, s2));

 

   s1 = null;

   s2 = "Tom";

   Console.WriteLine("Object.Equals(null, ""{1}"") => {2}",

       s1, s2, Object.Equals(s1, s2));

 

   s1 = "Carol";

   s2 = null;

   Console.WriteLine("Object.Equals(""{0}"", null) => {2}",

       s1, s2, Object.Equals(s1, s2));

 

   s1 = null;

   s2 = null;

   Console.WriteLine("Object.Equals(null, null) => {2}",

       s1, s2, Object.Equals(s1, s2));

   }

}

 

 

/*

 

This code produces the following output.

 

Object.Equals("Tom", "Carol") => False

Object.Equals("Tom", "Tom") => True

Object.Equals(null, "Tom") => False

Object.Equals("Carol", null) => False

Object.Equals(null, null) => True

 

*/

 

三、                  Format

根据指定格式将指定枚举类型的指定值转换为其等效的字符串表示形式

语法

[ComVisibleAttribute(true)]

public static string Format (

          Type enumType,

          Object value,

          string format

)

参数

enumType

要转换的值的枚举类型。

value

要转换的值。

format

要使用的输出格式。

返回值

value 的字符串表示形式。

备注

有效格式值有:

格式

说明

“G”或“g”

如果 value 等于某个已命名的枚举常数,则返回该常数的名称;否则返回 value 的等效十进制数。

例如,假定唯一的枚举常数命名为“Red”,其值为 1。如果将 value 指定为 1,则此格式返回“Red”。然而,如果将 value 指定为 2,则此格式返回“2”。

- 或 -

如果将 FlagsAttribute 自定义属性应用于枚举,则 value 将被视为位域,该位域包含一个或多个由一位或多位组成的标志。

如果 value 等于已命名的枚举常数的组合,则返回用分隔符分隔的这些常数名称的列表。将在 value 中搜索标志,从具有最大值的标志到具有最小值的标志进行搜索。对于与 value 中的位域相对应的每个标志,常数的名称连接到用分隔符分隔的列表。则将不再考虑该标记的值,而继续搜索下一个标志。

如果 value 不等于已命名的枚举常数的组合,则返回 value 的等效十进制数。

“X”或“x”

以十六进制形式表示 value(不带前导“0x”)。

“D”或“d”

以十进制形式表示 value。

“F”或“f”

对于“G”或“g”执行的行为是相同的,只是在 Enum 声明中不需要 FlagsAttribute。

示例

using System;

 

public class FormatTest {

    enum Colors { Red, Green, Blue, Yellow };

 

    public static void Main() {

        Colors myColor = Colors.Blue;

 

        Console.WriteLine("My favorite color is {0}", myColor);

        Console.WriteLine("The value of my favorite color is {0}", Enum.Format(typeof(Colors), myColor, "d"));

        Console.WriteLine("The hex value of my favorite color is {0}", Enum.Format(typeof(Colors), myColor, "x"));

    }

}

 

四、GetName

在指定枚举中检索具有指定值的常数的名称。

语法

[ComVisibleAttribute(true)]

public static string GetName (

          Type enumType,

          Object value

)

参数

enumType

枚举类型。

value

特定枚举常数的值(根据其基础类型)。

返回值

一个字符串,该字符串包含 enumType 的枚举常数的名称,该常数的值为 value;或者,如果没有找到这样的常数,则为空引用(在 Visual Basic 中为 Nothing)。

示例

using System;

 

public class GetNameTest {

    enum Colors { Red, Green, Blue, Yellow };

    enum Styles { Plaid, Striped, Tartan, Corduroy };

 

    public static void Main() {

 

        Console.WriteLine("The 4th value of the Colors Enum is {0}", Enum.GetName(typeof(Colors), 3));

        Console.WriteLine("The 4th value of the Styles Enum is {0}", Enum.GetName(typeof(Styles), 3));

    }

}

 

五、                  GetNames

检索指定枚举中常数名称的数组。

语法

[ComVisibleAttribute(true)]

public static string[] GetNames (

          Type enumType

)

参数

enumType

枚举类型。

返回值

enumType 的常数名称的字符串数组。

示例

using System;

 

public class GetNamesTest {

    enum Colors { Red, Green, Blue, Yellow };

    enum Styles { Plaid, Striped, Tartan, Corduroy };

 

    public static void Main() {

 

        Console.WriteLine("The values of the Colors Enum are:");

        foreach(string s in Enum.GetNames(typeof(Colors)))

            Console.WriteLine(s);

 

        Console.WriteLine();

 

        Console.WriteLine("The values of the Styles Enum are:");

        foreach(string s in Enum.GetNames(typeof(Styles)))

            Console.WriteLine(s);

    }

}

 

六、                  GetType

获取当前实例的 Type

语法

public Type GetType ()

返回值

Type 实例,表示当前实例的确切运行时类型。

示例

public class MyBaseClass: Object {

}

 

public class MyDerivedClass: MyBaseClass {

}

 

public class Test {

 

   public static void Main() {

      MyBaseClass myBase = new MyBaseClass();

      MyDerivedClass myDerived = new MyDerivedClass();

      object o = myDerived;

      MyBaseClass b = myDerived;

 

      Console.WriteLine("mybase: Type is {0}", myBase.GetType());

      Console.WriteLine("myDerived: Type is {0}", myDerived.GetType());

      Console.WriteLine("object o = myDerived: Type is {0}", o.GetType());

      Console.WriteLine("MyBaseClass b = myDerived: Type is {0}", b.GetType());

   }

}

 

 

/*

 

This code produces the following output.

 

mybase: Type is MyBaseClass

myDerived: Type is MyDerivedClass

object o = myDerived: Type is MyDerivedClass

MyBaseClass b = myDerived: Type is MyDerivedClass

 

*/

七、GetValues

检索指定枚举中常数值的数组。

语法

ComVisibleAttribute(true)]

public static Array GetValues (

          Type enumType

)

参数

enumType

枚举类型。

返回值

enumType 的常数值的 Array。该数组的元素按枚举常数的值排序。

示例

using System;

 

public class GetValuesTest {

    enum Colors { Red, Green, Blue, Yellow };

    enum Styles { Plaid = 0, Striped = 23, Tartan = 65, Corduroy = 78 };

 

    public static void Main() {

 

        Console.WriteLine("The values of the Colors Enum are:");

        foreach(int i in Enum.GetValues(typeof(Colors)))

            Console.WriteLine(i);

 

        Console.WriteLine();

 

        Console.WriteLine("The values of the Styles Enum are:");

        foreach(int i in Enum.GetValues(typeof(Styles)))

            Console.WriteLine(i);

    }

}

 

 源码下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值