Enum.Equals(Object) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
傳回值,該值表示這個執行個體是否和指定的物件相等。
public:
override bool Equals(System::Object ^ obj);
public override bool Equals (object obj);
public override bool Equals (object? obj);
override this.Equals : obj -> bool
Public Overrides Function Equals (obj As Object) As Boolean
參數
- obj
- Object
與這個執行個體相比較的物件,或 null
。
傳回
如果 obj
是與這個執行個體具有相同類型及基礎值的列舉值則為 true
;否則為 false
。
範例
在下列程式碼中,說明了如何使用 Equals 方法。
using namespace System;
public enum class Colors
{
Red, Green, Blue, Yellow
};
public enum class Mammals
{
Cat, Dog, Horse, Dolphin
};
int 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 ) ? (String^)"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 ) ? (String^)"Yes" : "No" );
Console::WriteLine();
Console::WriteLine( "The value of my color ({0}) is {1}", myColor, Enum::Format( Colors::typeid, myColor, "d" ) );
Console::WriteLine( "The value of my pet (a {0}) is {1}", myPet, Enum::Format( Mammals::typeid, myPet, "d" ) );
Console::WriteLine( "Even though they have the same value, are they equal? {0}", myColor.Equals( myPet ) ? (String^)"Yes" : "No" );
}
// 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
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");
}
}
// 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
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
下列範例會定義兩個列舉類型: SledDog
和 WorkDog
。 SledDog
列舉具有兩個 SledDog.AlaskanMalamute
SledDog.Malamute
具有相同基礎值的成員:和。 方法的呼叫 Equals 表示這些值相等,因為它們的基礎值相同。 SledDog.Malamute
和 WorkDog.Newfoundland
成員具有相同的基礎值,雖然它們代表不同的列舉類型。 方法的呼叫 Equals 表示這些值不相等。
using System;
public enum SledDog { Unknown=0, AlaskanMalamute=1, Malamute=1,
Husky=2, SiberianHusky=2 };
public enum WorkDog { Unknown=0, Newfoundland=1, GreatPyrennes=2 };
public class Example
{
public static void Main()
{
SledDog dog1 = SledDog.Malamute;
SledDog dog2 = SledDog.AlaskanMalamute;
WorkDog dog3 = 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));
}
}
// The example displays the following output:
// Malamute (1) = Malamute (1): True
// Malamute (1) = Newfoundland (1): False
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
備註
Enum.Equals(Object)方法會覆寫 ValueType.Equals(Object) 以定義列舉成員如何評估是否相等。