Skip to content

Instantly share code, notes, and snippets.

@princeppy
Created February 26, 2018 05:06
Show Gist options
  • Save princeppy/228c70876c9aa50b213d6958fd5741d9 to your computer and use it in GitHub Desktop.
Save princeppy/228c70876c9aa50b213d6958fd5741d9 to your computer and use it in GitHub Desktop.

I Call It as BinaryFlagedEnums

Imagin that you have a class like this
class User {
    .....
    .....
    bool CanDelete;
    bool CanRead;
    bool CanWrite;
    bool CanModify;
    bool CanCreate;
}
How can we combine all of those permissions into a single value
[Flags]
enum PermissionTypes : int {
    None = 0,
    Read = 1,
    Write = 2,
    Modify = 4,
    Delete = 8
    Create = 16,
    All = Read | Write | Modify | Delete | Create
}

//and the class from before
class User {
    PermissionTypes Permissions = PermissionTypes.None;
}
so now what?
//create a new user
User admin = new User();
admin.Permissions = PermissionTypes.Read 
    | PermissionTypes.Write 
    | PermissionTypes.Delete;

//check for permissions
bool canRead = ((PermissionTypes.Read & admin.Permissions) == PermissionTypes.Read);
bool canWrite = ((PermissionTypes.Write & admin.Permissions) == PermissionTypes.Write);
bool canCreate = ((PermissionTypes.Create & admin.Permissions) == PermissionTypes.Create);

//and the results
Console.WriteLine(canRead); //true
Console.WriteLine(canWrite); //true
Console.WriteLine(canCreate); //false
Lets simplify little more...
namespace Enum.Extensions
{
    public static class EnumerationExtensions
    {
        //checks if the value contains the provided type
        public static bool Has<T>(this System.Enum type, T value)
        {
            try { return (((int)(object)type & (int)(object)value) == (int)(object)value); }
            catch { return false; }
        }

        //checks if the value is only the provided type
        public static bool Is<T>(this System.Enum type, T value)
        {
            try { return (int)(object)type == (int)(object)value; }
            catch { return false; }
        }

        //appends a value
        public static T Add<T>(this System.Enum type, T value)
        {
            try { return (T)(object)(((int)(object)type | (int)(object)value)); }
            catch (Exception ex)
            {
                throw new ArgumentException(string.Format("Could not append value from enumerated type '{0}'.",typeof(T).Name), ex);
            }
        }

        //completely removes the value
        public static T Remove<T>(this System.Enum type, T value)
        {
            try { return (T)(object)(((int)(object)type & ~(int)(object)value)); }
            catch (Exception ex)
            {
                throw new ArgumentException(string.Format("Could not remove value from enumerated type '{0}'.",typeof(T).Name), ex);
            }
        }

    }
}
and...
//start with a value
PermissionTypes permissions = PermissionTypes.Read | PermissionTypes.Write;

//then check for the values
bool canRead = permissions.Has(PermissionTypes.Read); //true
bool canWrite = permissions.Has(PermissionTypes.Write); //true
bool canDelete = permissions.Has(PermissionTypes.Delete); //false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment