TICS Coding Standard Viewer 
TIOBE Software Quality Framework
Print-friendly version
©TIOBE Software www.tiobe.com
C# Coding Standard

Rule:  10@203

Synopsis:Use the [Flags] attribute on an enum if a bitwise operation is to be performed on the numeric values
Severity Level:7
Category:Data types


It is good practice to use the Flags attribute for documenting that the enumeration is intended for combinations. Also using this attribute provides an implementation of the ToString method, which displays the values in their original names instead of the values.


FileInfo file = new FileInfo(fileName);
file.Attributes = FileAttributes.Hidden | FileAttributes.ReadOnly;
Console.WriteLine("file.Attributes = {0}", file.Attributes.ToString());
The printed result will be ReadOnly|Hidden.

Use an enum with the flags attribute only if the value can be completely expressed as a set of bit flags. Do not use an enum for open sets (such as the operating system version). Use a plural name for such an enum, as stated in [3@203].


public enum AccessPrivileges
	Read   = 0x1,
	Write  = 0x2,
	Append = 0x4,
	Delete = 0x8,
	All    = Read | Write | Append | Delete