Enum.Equals(Object) Método
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Devolve um valor que indica se esta instância é igual a um objeto especificado.
public:
override bool Equals(System::Object ^ obj);
public override bool Equals(object obj);
override this.Equals : obj -> bool
Public Overrides Function Equals (obj As Object) As Boolean
Parâmetros
- obj
- Object
Um objeto para comparar com esta instância, ou null.
Devoluções
true se obj for um valor de enumeração do mesmo tipo e com o mesmo valor subjacente que esta instância; caso contrário, false.
Exemplos
O exemplo seguinte ilustra a utilização do Equals método.
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
open System
type Colors =
| Red = 0
| Green = 1
| Blue = 2
| Yellow = 3
type Mammals =
| Cat = 0
| Dog = 1
| Horse = 2
| Dolphin = 3
let myPet = Mammals.Cat
let myColor = Colors.Red
let yourPet = Mammals.Dog
let yourColor = Colors.Red
printfn
$"""My favorite animal is a {myPet}
Your favorite animal is a {yourPet}
Do we like the same animal? {if myPet.Equals yourPet then "Yes" else "No"}
My favorite color is {myColor}
Your favorite color is {yourColor}
Do we like the same color? {if myColor.Equals yourColor then "Yes" else "No"}
The value of my color ({myColor}) is {Enum.Format(typeof<Colors>, myColor, "d")}
The value of my pet (a {myPet}) is {Enum.Format(typeof<Mammals>, myPet, "d")}
Even though they have the same value, are they equal? {if myColor.Equals myPet then "Yes" else "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 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
O exemplo seguinte define dois tipos de enumeração, SledDog e WorkDog. A SledDog enumeração tem dois membros, SledDog.AlaskanMalamute e SledDog.Malamute, que têm o mesmo valor subjacente. A chamada ao Equals método indica que estes valores são iguais porque os seus valores subjacentes são os mesmos. Os SledDog.Malamute membros e WorkDog.Newfoundland têm o mesmo valor subjacente, embora representem tipos de enumeração diferentes. Uma chamada ao Equals método indica que estes valores não são iguais.
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
type SledDog =
| Unknown = 0
| AlaskanMalamute = 1
| Malamute = 1
| Husky = 2
| SiberianHusky = 2
type WorkDog =
| Unknown = 0
| Newfoundland = 1
| GreatPyrennes = 2
let dog1 = SledDog.Malamute
let dog2 = SledDog.AlaskanMalamute
let dog3 = WorkDog.Newfoundland
printfn $"{dog1:F} ({dog1:D}) = {dog2:F} ({dog2:D}): {dog1.Equals dog2}"
printfn $"{dog1:F} ({dog1:D}) = {dog3:F} ({dog3:D}): {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
Observações
O Enum.Equals(Object) método sobrepõe-se ValueType.Equals(Object) para definir como os membros da enumeração são avaliados quanto à igualdade.