Export (0) Print
Expand All

Enum.Equals Method

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


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";


Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

© 2015 Microsoft