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


C# Enum.Equals方法代码示例

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


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

示例1: Main

//引入命名空间
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");
    }
}
开发者ID:.NET开发者,项目名称:System,代码行数:29,代码来源:Enum.Equals

输出:

My favorite animal is a Cat
Your favorite animal is a Dog
Do we like the same animal? No

My favorite color is Red
Your favorite color is Red
Do we like the same color? Yes

The value of my color (Red) is 0
The value of my pet (a Cat) is 0
Even though they have the same value, are they equal? No

示例2: Main

//引入命名空间
using System;

public enum SledDog { Unknown=0, AlaskanMalamute=1, Malamute=1, 
                      Husky=2, SiberianHusky=2 };

public enum WorkDog { Unknown=0, Newfoundland=1, GreatPyrennes=2 };

public class Example
{
   public static void Main()
   {
      SledDog dog1 = SledDog.Malamute;
      SledDog dog2 = SledDog.AlaskanMalamute;
      WorkDog dog3 = WorkDog.Newfoundland;
      
      Console.WriteLine("{0:F} ({0:D}) = {1:F} ({1:D}): {2}", 
                        dog1, dog2, dog1.Equals(dog2));
      Console.WriteLine("{0:F} ({0:D}) = {1:F} ({1:D}): {2}",
                        dog1, dog3, dog1.Equals(dog3));
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:22,代码来源:Enum.Equals

输出:

Malamute (1) = Malamute (1): True
Malamute (1) = Newfoundland (1): False


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