Articles → CSHARP → Flags Attribute In Enums C#
Flags Attribute In Enums C#
Software Requirement
What Is The Purpose Of The Flags Attribute?
Syntax
[Flags]
public enum Enum_Name {}
Example
- Above 18 years of age
- A resident of that country
- A graduate
- Without criminal background
public bool IsEligibleToContestElection(bool above18, bool resident, bool graduate, bool nocriminalbackground) {
if (above18 == true && resident == true && graduate == true && nocriminalbackground == true) return true;
else return false;
}
IsEligibleToContestElection(true, true, true, true)
[Flags]
public enum EligibilityCriteria {
Above18,
IsResident,
AGraduate,
NoCriminalBackground
}
public bool IsEligibleToContestElection(EligibilityCriteria criteria) {
if (criteria == (EligibilityCriteria.Above18 & EligibilityCriteria.IsResident & EligibilityCriteria.AGraduate & EligibilityCriteria.NoCriminalBackground)) return true;
else return false;
}
IsEligibleToContestElection(EligibilityCriteria.Above18 & EligibilityCriteria.IsResident & EligibilityCriteria.AGraduate & EligibilityCriteria.NoCriminalBackground)