C#枚举等于方法
要找到枚举之间的相等性,请使用Equals()方法。
假设我们有以下枚举。
enum Products { HardDrive, PenDrive, Keyboard};创建两个Products对象并分配相同的值。
Products prod1 = Products.HardDrive; Products prod2 = Products.HardDrive;
现在使用Equals()method检查是否相等。这是正确的,因为两者具有相同的基础值。
示例
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
热门推荐
5 短祝福语简短暖心
10 结婚祝福语粤语大全简短
11 晚上祝福语女生文案简短
12 法语妈妈生日祝福语简短
13 药厂开工祝福语大全简短
14 蛋糕节日祝福语简短英文
15 跨年的生日祝福语简短
16 文案祝福语英文短句简短
17 在家聚餐婚礼祝福语简短
18 学生节祝福语大全简短