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

Rule:  10@203

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


Description:

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.

Example:

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].

Example:

[Flags]
public enum AccessPrivileges
{
	Read   = 0x1,
	Write  = 0x2,
	Append = 0x4,
	Delete = 0x8,
	All    = Read | Write | Append | Delete
}