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)

[SecuritySafeCriticalAttribute]
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";
   }
}


Silverlight

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.

Community Additions

ADD
Show:
© 2014 Microsoft