Tuesday, January 22, 2013

C# Flags Attribute

The FlagsAttribute is an attribute that can be placed on your enumeration in order to be comparable with multiple enumeration values. Usually, this attribute uses bitwise operation in the comparison. Please visit Microsoft documentation for further details.

Please visit C# Bitwise blog for you to understand further the bitwise operation.

Here we created a very simple enumeration named Privilege that enumerate the kind of privileges we can give to every user of our website.

[FlagsAttribute]
public enum Privilege : short
{
    Read = 1,
    Write = 2,
    Delete = 4,
    Create = 8,
    Share = 16,
    Append = 32,
    Assign = 64
}

You will see above we declared the Privilege enumeration with FlagsAttribute. Unfortunately in order the value of the enumerations to be participated in bitwise operation we need to declare its value in proper decimal value. In our case, we need to enumerate the value 1, 2, 4, 8, 16, 32 and so on.

Now, using this enumeration in your class can now be checked with bitwise comparison. See our class below declaring the Privilege class.

public class User
{
    public User(string username)
    {
        this.Username = username;
    }

    public string Username
    {
        get;
        private set;
    }

    public Privilege Privilege
    {
        get;
        set;
    }
}

The class declared above named User has property Username of type String and Privilege of type Privilege. Now using this class, we can set the bitwise value of the Privilege property. See the codes below how to do it.

var user = new User(System.Security.Principal.WindowsIdentity.GetCurrent().Name);
user.Privilege = (Privilege.Read | Privilege.Write | Privilege.Delete | Privilege.Create);

What the above code is doing is, it has created a new object User passing the current Windows Log On name. After that, it set the Privilege property value to Read, Write, Delete and Create. With the help of the Flags attribute, we now participated in bitwise operation by the use of the "|" operator. This mean that the privilege property has now the value of the Privilege enum's Read, Write, Delete and Create.

Checking the presence of the Enumeration value in bitmap operation is very easy in C#. We are now required to use the "&" operator to address it. See the code below.

if ((user.Privilege & Privilege.Delete) == Privilege.Delete)
{
    /* do the delete code */
}
else if ((user.Privilege & Privilege.Append) == Privilege.Append)
{
    /* do the append code */
}
else
{
    /* do nothing */
}

What the code doing above is to simply determine whether the Privilege property has the defined Privilege enumeration value. In short, we are asking if the User.Privilege property has Privilege.Delete value.

Narrowing Bitwise Operations

The operator "-" is used to narrow the values of the Bitwise operation. See the code below how to do it.

user.Privilege -= Privilege.Delete;

With the code above, we are removing the Delete Privilege value from the User.Privilege property. You will then see that the only remaining bitwise value for the User.Privilege property is Read, Write and Create.

Widening Bitwise Operations

The operator "|" is used in widening of the Bitwise operation. See the code below how to do it.

user.Privilege |= Privilege.Assign;

Same with narrowing, what we did is that we simply added a new Privilege enumeration value Assign in to the User.Privilege property. By checking the User.Privilege property value is now Read, Write, Create and Assign.

Now that you know how to do it, have a happy coding. Tweak tweak tweak your codes now ;)

No comments:

Post a Comment

Place your comments and ideas