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)

'Declaration
Public Overrides Function Equals ( _
	obj As Object _
) As Boolean

Parameters

obj
Type: System.Object

An object to compare with this instance, or Nothing.

Return Value

Type: System.Boolean
true if obj is an enumeration value of the same type and with the same underlying value as this instance; otherwise, 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.

Public Class EqualsTest
    Enum Colors
        Red
        Green
        Blue
        Yellow
    End Enum  

    Enum Mammals
        Cat
        Dog
        Horse
        Dolphin
    End Enum  

    Public Shared Sub Main()
        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

        Console.WriteLine("My favorite animal is a {0}", myPet)
        Console.WriteLine("Your favorite animal is a {0}", yourPet)
        If myPet.Equals(yourPet) Then output = "Yes" Else output = "No"
        Console.WriteLine("Do we like the same animal? {0}", output)

        Console.WriteLine()
        Console.WriteLine("My favorite color is {0}", myColor)
        Console.WriteLine("Your favorite color is {0}", yourColor)
        If myColor.Equals(yourColor) Then output = "Yes" Else output = "No"
        Console.WriteLine("Do we like the same color? {0}", output)

        Console.WriteLine()
        Console.WriteLine("The value of my color ({0}) is {1}", myColor, [Enum].Format(GetType(Colors), myColor, "d"))
        Console.WriteLine("The value of my pet (a {0}) is {1}", myPet, [Enum].Format(GetType(Mammals), myPet, "d"))
        Console.WriteLine("Even though they have the same value, are they equal? {0}", 
                          If(myColor.Equals(myPet), "Yes", "No"))
    End Sub  
End Class  
' 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.

Public Enum SledDog As Integer
   Unknown=0
   AlaskanMalamute=1
   Malamute=1
   Husky=2
   SiberianHusky=2
End Enum 

Public Enum WorkDog As Integer
   Unknown=0
   Newfoundland=1
   GreatPyrennes=2
End Enum 

Module Example
   Public Sub Main()
      Dim dog1 As SledDog = SledDog.Malamute
      Dim dog2 As SledDog = SledDog.AlaskanMalamute
      Dim dog3 As WorkDog = 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))
   End Sub 
End Module 
' The example displays the following output: 
'       Malamute (1) = Malamute (1): True 
'       Malamute (1) = Newfoundland (1): False

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft