Export (0) Print
Expand All

Enum.Equals Method (Object)

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
)
public boolean Equals (
	Object obj
)
public override function Equals (
	obj : Object
) : boolean
Not applicable.

Parameters

obj

An object to compare with this instance, or a null reference (Nothing in Visual Basic).

Return Value

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 EqualsTest {
    enum Colors { Red, Green, Blue, Yellow };
    enum Mammals { Cat, Dog, Horse, Dolphin };

    public static void Main() {
        Mammals myPet = Mammals.Cat;
        Colors myColor = Colors.Red;
        Mammals yourPet = Mammals.Dog;
        Colors yourColor = Colors.Red;

        Console.WriteLine("My favorite animal is a {0}", myPet);
        Console.WriteLine("Your favorite animal is a {0}", yourPet);
        Console.WriteLine("Do we like the same animal? {0}", myPet.Equals(yourPet) ? "Yes" : "No");

        Console.WriteLine();
        Console.WriteLine("My favorite color is {0}", myColor);
        Console.WriteLine("Your favorite color is {0}", yourColor);
        Console.WriteLine("Do we like the same color? {0}", myColor.Equals(yourColor) ? "Yes" : "No");

        Console.WriteLine();
        Console.WriteLine("The value of my color ({0}) is {1}", myColor, Enum.Format(typeof(Colors), myColor, "d"));
        Console.WriteLine("The value of my pet (a {0}) is {1}", myPet, Enum.Format(typeof(Mammals), myPet, "d"));
        Console.WriteLine("Even though they have the same value, are they equal? {0}", 
                    myColor.Equals(myPet) ? "Yes" : "No");
    }
}

import System.*;

public class EqualsTest
{
    enum Colors
    {
        red (0),
        green (1),
        blue (2),
        yellow (3);
    } //Colors

    enum Mammals
    {
        cat (0),
        dog (1),
        horse (2),
        dolphin (3);
    } //Mammals

    public static void main(String[] args)
    {
        Mammals myPet = Mammals.cat;
        Colors myColor = Colors.red;
        Mammals yourPet = Mammals.dog;
        Colors yourColor = Colors.red;

        Console.WriteLine("My favorite animal is a {0}", myPet);
        Console.WriteLine("Your favorite animal is a {0}", yourPet);
        Console.WriteLine("Do we like the same animal? {0}", 
            (myPet.Equals(yourPet)) ? "Yes" : "No");

        Console.WriteLine();
        Console.WriteLine("My favorite color is {0}", myColor);
        Console.WriteLine("Your favorite color is {0}", yourColor);
        Console.WriteLine("Do we like the same color? {0}", 
            (myColor.Equals(yourColor)) ? "Yes" : "No");

        Console.WriteLine();
        Console.WriteLine("The value of my color ({0}) is {1}", myColor, 
            Enum.Format(Colors.class.ToType(), myColor, "d"));
        Console.WriteLine("The value of my pet (a {0}) is {1}", myPet, 
            Enum.Format(Mammals.class.ToType(), myPet, "d"));
        Console.WriteLine("Even though they have the same value, "
            + "are they equal? {0}", (myColor.Equals(myPet)) ? "Yes" : "No");
    } //main
} //EqualsTest

import System;

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

    public static function Main() {
        var myPet : Mammals = Mammals.Cat;
        var myColor : Colors = Colors.Red;
        var yourPet : Mammals = Mammals.Dog;
        var yourColor : Colors = Colors.Red;

        Console.WriteLine("My favorite animal is a {0}", myPet);
        Console.WriteLine("Your favorite animal is a {0}", yourPet);
        Console.WriteLine("Do we like the same animal? {0}", myPet.Equals(yourPet) ? "Yes" : "No");

        Console.WriteLine();
        Console.WriteLine("My favorite color is {0}", myColor);
        Console.WriteLine("Your favorite color is {0}", yourColor);
        Console.WriteLine("Do we like the same color? {0}", myColor.Equals(yourColor) ? "Yes" : "No");

        Console.WriteLine();
        Console.WriteLine("The value of my color ({0}) is {1}", myColor, Enum.Format(Colors, myColor, "d"));
        Console.WriteLine("The value of my pet (a {0}) is {1}", myPet, Enum.Format(Mammals, myPet, "d"));
        Console.WriteLine("Even though they have the same value, are they equal? {0}", 
                    myColor.Equals(myPet) ? "Yes" : "No");
    }
}

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft