Dieser Artikel wurde maschinell übersetzt. Wenn Sie die englische Version des Artikels anzeigen möchten, aktivieren Sie das Kontrollkästchen Englisch. Sie können den englischen Text auch in einem Popupfenster anzeigen, indem Sie den Mauszeiger über den Text bewegen.
Übersetzung
Englisch

Enum.Equals-Methode (Object)

 

Gibt einen Wert zurück, der angibt, ob diese Instanz gleich einem angegebenen Objekt ist.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

public override bool Equals(
	object obj
)

Parameter

obj

Ein Objekt, das mit dieser Instanz verglichen werden soll, oder null.

Rückgabewert

Type: System.Boolean

true, wenn obj ein Enumerationswert des gleichen Typs und mit dem gleichen zugrunde liegenden Wert wie diese Instanz ist, andernfalls false.

The Enum.Equals(Object) method overrides ValueType.Equals(Object) to define how enumeration members are evaluated for equality.

The following example illustrates the use of the Equals method.

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

The following example defines two enumeration types, SledDog and WorkDog.The SledDog enumeration has two members, SledDog.AlaskanMalamute and SledDog.Malamute, that have the same underlying value.The call to the Equals method indicates that these values are equal because their underlying values are the same.The SledDog.Malamute and WorkDog.Newfoundland members have the same underlying value, although they represent different enumeration types.A call to the Equals method indicates that these values are not equal.

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

Universal Windows Platform
Verfügbar seit 4.5
.NET Framework
Verfügbar seit 1.1
Portable Class Library
Unterstützt in: portable .NET platforms
Silverlight
Verfügbar seit 2.0
Windows Phone Silverlight
Verfügbar seit 7.0
Windows Phone
Verfügbar seit 8.1
Zurück zum Anfang
Anzeigen: