Compartir a través de


Enum.Equals Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Overrides Function Equals ( _
    obj As Object _
) As Boolean
[SecuritySafeCriticalAttribute]
public override bool Equals(
    Object obj
)

Parameters

  • obj
    Type: System.Object
    An object to compare with this instance, or nulla null reference (Nothing in Visual Basic).

Return Value

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

Examples

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


Public Class Example

   Enum Colors
      Red
      Green
      Blue
      Yellow
   End Enum 'Colors

   Enum Mammals
      Cat
      Dog
      Horse
      Dolphin
   End Enum 'Mammals

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim myPet As Mammals = Mammals.Cat
      Dim myColor As Colors = Colors.Red
      Dim yourPet As Mammals = Mammals.Dog
      Dim yourColor As Colors = Colors.Red
      Dim output As String

      outputBlock.Text += String.Format("My favorite animal is a {0}", myPet) & vbCrLf
      outputBlock.Text += String.Format("Your favorite animal is a {0}", yourPet) & vbCrLf
      If myPet.Equals(yourPet) Then output = "Yes" Else output = "No"
      outputBlock.Text += String.Format("Do we like the same animal? {0}", output) & vbCrLf

      outputBlock.Text &= vbCrLf
      outputBlock.Text += String.Format("My favorite color is {0}", myColor) & vbCrLf
      outputBlock.Text += String.Format("Your favorite color is {0}", yourColor) & vbCrLf
      If myColor.Equals(yourColor) Then output = "Yes" Else output = "No"
      outputBlock.Text += String.Format("Do we like the same color? {0}", output) & vbCrLf

      outputBlock.Text &= vbCrLf
      outputBlock.Text += String.Format("The value of my color ({0}) is {1}", myColor, myColor.ToString("d")) & vbCrLf
      outputBlock.Text += String.Format("The value of my pet (a {0}) is {1}", myPet, myPet.ToString("d")) & vbCrLf
      If myColor.Equals(myPet) Then output = "Yes" Else output = "No"
      outputBlock.Text += String.Format("Even though they have the same value, are they equal? {0}", output) & vbCrLf
   End Sub 'Main
End Class 'EqualsTest
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";
   }
}

Version Information

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

Platforms

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