Bit Flagged Enum Types in C#
Elegance in programming is not optional - Richard A O’Keefe
When I craft software, naming classes and variables is one of the toughest task. I spend considerable amount of time for naming a solution, projects, classes and variables. I strongly believe that a good naming convention makes the code more elegant, organized, readable and maintainable. Usually, my code review process results more naming comments
After I started coding in C#, I was never satisfied whenever I handled bit flagged enum types, not in terms of writing the core bitwise logic, but the naming and structuring the code. Of course, I was pretty much comfortable coding the same in C++. I found a clean approach to handle bit flagged enum types. Have a look.
How to use..
try
{
FileEx file = new FileEx();
// Add three attributes to the file
file.Attributes += Attribute.ReadOnly;
file.Attributes += Attribute.Archive;
file.Attributes += Attribute.Compressed;
// Check whether or not the file is ReadOnly
if (file.Attributes.Contains(Attribute.ReadOnly))
{
Debug.WriteLine("Yes.. File is ReadOnly");
}
// Check whether or not the file is ReadOnly and Compressed
if (file.Attributes.Contains(Attribute.ReadOnly) &&
file.Attributes.Contains(Attribute.Compressed))
{
Debug.WriteLine("Yes.. Files is ReadOnly and Compressed");
}
// Remove ReadOnly attribute
file.Attributes -= Attribute.ReadOnly;
// Check whether or not the file is ReadOnly
if (!file.Attributes.Contains(Attribute.ReadOnly))
{
Debug.WriteLine("Oops.. Looks like some made this File writable");
}
// Remove ReadOnly attribute
file.Attributes += Attribute.ReadOnly;
// (or) we can use the FileEx property
if (file.IsReadOnly)
{
Debug.WriteLine("Yes.. File is read only now");
}
}
catch (Exception)
{
// Do something for handling exceptions
}
Here is the code..
/// <summary>
/// ‘Attribute’ enum type holds various types file attributes
/// </summary>
public enum Attribute
{
None = 0×00 /* 00000000 */,
ReadOnly = 0×01 /* 00000001 */,
Hidden = 0×02 /* 00000010 */,
System = 0×04 /* 00000100 */,
Archive = 0×08 /* 00001000 */,
Compressed = 0×10 /* 00010000 */,
Encrypted = 0×20 /* 00100000 */,
Directory = 0×40 /* 01000000 */,
Device = 0×80 /* 10000000 */,
}
/// <summary>
/// ‘Attributes’ is wrapper class to manipulate bitwise enum operations
/// </summary>
public class Attributes
{
private Attribute _items;
private Attribute Items
{
get { return _items; }
set { _items = value; }
}
public Attributes()
{
this.Items = Attribute.None;
}
public Attributes(Attribute attribute)
{
this.Items = attribute;
}
public bool Contains(Attribute attribute)
{
return (((int)(this.Items & attribute)) != 0);
}
public static Attributes operator +(Attributes source, Attribute attribute)
{
return new Attributes(source.Items | attribute);
}
public static Attributes operator -(Attributes source, Attribute attribute)
{
return new Attributes(source.Items & ~attribute);
}
}
/// <summary>
/// A FileEx class to demonstrate how to use ‘Attributes’
/// </summary>
public class FileEx
{
public Attributes Attributes { get; set; }
public FileEx()
{
this.Attributes = new Attributes();
}
public bool IsReadOnly
{
get { return this.Attributes.Contains(Attribute.ReadOnly); }
}
}
Compiler : VS2008 on .NET 3.5
Finally..
Sometime back, I used bit flagged enum types in WCF. I was facing an issue due to improperly handled flagged enum type which swallowed tons of my time.. So be aware and careful.
The following two things make me more happy in this code
- A Singularized name for ‘Attribute’ enum type
- A Pluralized name for ‘Attributes’ wrapper class
I really enjoyed writing this code. I am pretty sure, a developer who loves code simply loves this code too. Extend this code as you like..
Exercises..
1. Try investigate how, when and where to use bitwise shift operators.
2. Add a ‘[Flag]’ attribute to ‘Attribute’ enum type as shown below and debug through the code. What is the benefit of using ‘[Flag]’ attribute?
[Flags]
public enum Attribute
3. How to write the following code without type cast?
(((int)(this.Items & flag)) != 0)
-Xavier Robinston Antony Sunday, 22 August, 2010
























Recent Comments