Created
April 19, 2012 08:19
-
-
Save toepoke/2419597 to your computer and use it in GitHub Desktop.
Illustrates using Extensions with Enumeration types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
namespace EnumsAndExtensions { | |
public enum MemberState { | |
Empty = 0, | |
Joined = 1, | |
ToBeVerified = 2, | |
Declined = 3, | |
Rejected = 4, | |
Suspended = 5 | |
} | |
public static class EnumExtensions { | |
public static bool Has(this MemberState ms, params MemberState[] possibilities) { | |
return possibilities.Contains(ms); | |
} | |
public static bool NotAMember(this MemberState ms) { | |
return ms.Has(MemberState.ToBeVerified, MemberState.Declined, MemberState.Rejected, MemberState.Suspended); | |
} | |
} | |
public class Program { | |
static void Main(string[] args) { | |
MemberState bobFluqi = MemberState.Rejected; | |
// Would you rather write ... | |
if (bobFluqi == MemberState.ToBeVerified || bobFluqi == MemberState.Declined || bobFluqi == MemberState.Rejected || bobFluqi == MemberState.Suspended) { | |
Console.WriteLine("bobFluqi is not a member."); | |
} | |
// or ... | |
if (bobFluqi.NotAMember()) | |
Console.WriteLine("bobFluqi is not a member."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment