Skip to content

Instantly share code, notes, and snippets.

@toepoke
Created April 19, 2012 08:19
Show Gist options
  • Save toepoke/2419597 to your computer and use it in GitHub Desktop.
Save toepoke/2419597 to your computer and use it in GitHub Desktop.
Illustrates using Extensions with Enumeration types
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