Equals Method

Enum.Equals Method

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Returns a value indicating whether this instance is equal to a specified object.

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

public override bool Equals(
	Object obj
)

Parameters

obj
Type: System.Object
An object to compare with this instance, or null.

Return Value

Type: System.Boolean
true if obj is an Enum with the same underlying type and value as this instance; otherwise, false.

The following example illustrates the use of Equals in the context of Enum.


using System;

public class Example
{
   enum Colors { Red, Green, Blue, Yellow };
   enum Mammals { Cat, Dog, Horse, Dolphin };

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      Mammals myPet = Mammals.Cat;
      Colors myColor = Colors.Red;
      Mammals yourPet = Mammals.Dog;
      Colors yourColor = Colors.Red;

      outputBlock.Text += String.Format("My favorite animal is a {0}", myPet) + "\n";
      outputBlock.Text += String.Format("Your favorite animal is a {0}", yourPet) + "\n";
      outputBlock.Text += String.Format("Do we like the same animal? {0}", myPet.Equals(yourPet) ? "Yes" : "No") + "\n";

      outputBlock.Text += "\n";
      outputBlock.Text += String.Format("My favorite color is {0}", myColor) + "\n";
      outputBlock.Text += String.Format("Your favorite color is {0}", yourColor) + "\n";
      outputBlock.Text += String.Format("Do we like the same color? {0}", myColor.Equals(yourColor) ? "Yes" : "No") + "\n";

      outputBlock.Text += "\n";
      outputBlock.Text += String.Format("The value of my color ({0}) is {1}", myColor, myColor.ToString("d")) + "\n";
      outputBlock.Text += String.Format("The value of my pet (a {0}) is {1}", myPet, myPet.ToString("d")) + "\n";
      outputBlock.Text += String.Format("Even though they have the same value, are they equal? {0}",
                  myColor.Equals(myPet) ? "Yes" : "No") + "\n";
   }
}


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2017 Microsoft