tiplerinin listesini asla değiştirirse, bunu yapabilirsiniz:
NotificationMethodType allTypes = NotificationMethodType.Email |
NotificationMethodType.Fax |
NotificationMethodType.Sms;
NotificationMethodType notAssigned = allTypes & ~types;
~ tüm bitleri ters yüz edilerek, ters bir değer oluşturur.
enum iki yeni isimler dahil etmek olurdu, en azından enum Yerel "allTypes" tanımı tutmak için böyle çeteleler tanımlamak için tipik bir yol:
[Flags]
public enum NotificationMethodType {
None = 0,
Email = 1,
Fax = 2,
Sms = 4,
All = Email | Fax | Sms
}
Not: Gidersen enum All
değer katmanın yolu, types
boş olsaydı, bir enum almak değildir unutmayın o olur, bunun yerine "All" olarak "E-posta, faks, Sms" olarak baskı.
el allTypes
listesini korumak istemiyorsanız, sen Enum.GetValues
yöntemini kullanarak bunu yapabilirsiniz:
NotificationMethodType allTypes = 0;
foreach (NotificationMethodType type in Enum.GetValues(typeof(NotificationMethodType)))
allTypes |= type;
veya LINQ ile aynı yapabilirsiniz:
NotificationMethodType allTypes =
Enum.GetValues(typeof(NotificationMethodType))
.Cast<NotificationMethodType>()
.Aggregate ((current, value) => current | value);
Bu, allType
değerini, enum'un tüm bireysel değerlerini birlikte OR'ing yaparak oluşturur. Basit bir XOR hile olacaktır
FWIW, XOR (^ operatörler) kırma ve Lasse adlı 'değil de' olmadan enum şeyler ekleyebilir matematiksel olarak aynı şeydir. –