열거형 간의 동등성을 찾으려면 Equals() 메서드를 사용하십시오.
다음 Enum이 있다고 가정해 보겠습니다.
enum Products { HardDrive, PenDrive, Keyboard}; 두 개의 제품 개체를 만들고 동일한 값을 할당합니다.
Products prod1 = Products.HardDrive; Products prod2 = Products.HardDrive;
이제 Equals() 메서드를 사용하여 같음을 확인합니다. 둘 다 동일한 기본 값을 가지므로 True일 것입니다.
예시
using System;
class Program {
enum Products {HardDrive, PenDrive, Keyboard};
enum ProductsNew { Mouse, HeadPhone, Speakers};
static void Main() {
Products prod1 = Products.HardDrive;
Products prod2 = Products.HardDrive;
ProductsNew newProd1 = ProductsNew.HeadPhone;
ProductsNew newProd2 = ProductsNew.Speakers;
Console.WriteLine("Both are same products = {0}", prod1.Equals(prod2) ? "Yes" : "No");
Console.WriteLine("Both are same products = {0}", newProd1.Equals(newProd2) ? "Yes" : "No");
}
} 출력
Both are same products = Yes Both are same products = No