英語で読む

次の方法で共有


Enum.Equals(Object) メソッド

定義

このインスタンスが指定されたオブジェクトに等しいかどうかを示す値を返します。

C#
public override bool Equals(object obj);
C#
public override bool Equals(object? obj);

パラメーター

obj
Object

対象のインスタンスと比較する対象のオブジェクト、または null

戻り値

obj が同じ型の列挙体値で、基になる値がこのインスタンスと同じである場合は true、それ以外の場合は false です。

Equals メソッドの使用例を次に示します。

C#
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");
    }
}
// The example displays the following output:
//    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

次の例では、SledDogWorkDogという 2 つの列挙型を定義しています。 SledDog列挙体には、SledDog.AlaskanMalamuteSledDog.Malamuteという 2 つのメンバーがあり、基になる値が同じです。 Equalsメソッドの呼び出しは、基になる値が同じであるため、これらの値が等しいことを示しています。 SledDog.MalamuteメンバーとWorkDog.Newfoundlandメンバーは、基になる値が同じですが、異なる列挙型を表しています。 Equalsメソッドの呼び出しは、これらの値が等しくないことを示します。

C#
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));
   }
}
// The example displays the following output:
//       Malamute (1) = Malamute (1): True
//       Malamute (1) = Newfoundland (1): False

注釈

Enum.Equals(Object)メソッドは、列挙メンバーが等しいかどうかを評価する方法を定義するためにValueType.Equals(Object)をオーバーライドします。

適用対象

製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

こちらもご覧ください