Moh*_*Ali 10 .net c# enums enum-flags
我注意到这两个模式用于检查枚举标志:
[Flags]
public enum PurchaseType
{
None = 0,
SalePrice = 2,
RegularPrice = 4,
Clearance = 8,
CreditCard = 16
}
public void Test()
{
PurchaseType type = PurchaseType.Clearance;
type |= PurchaseType.CreditCard;
// Practice 1
if ((type & PurchaseType.Clearance) == PurchaseType.Clearance)
{
// Clearance item handling
}
// Practice 2
if ((type & PurchaseType.CreditCard) != 0)
{
// Credit card item handling
}
}
Run Code Online (Sandbox Code Playgroud)
在检查枚举标志的两种方法中,哪一种更好,性能,可读性,代码健康以及我应该做出的任何其他考虑?
谢谢,穆罕默德
Kam*_*eri 15
.Net 4引入了一种HasFlag
方法,用于确定当前实例中是否设置了一个或多个位字段,这是迄今为止最佳实践:
type.HasFlag(PurchaseType.CreditCard); // true
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2064 次 |
最近记录: |