Flags

Besides their use for discrete named constants, enums can also be used to describe flags. For example, System.IO defines the FileOpen enum as follows:

image

This nonflags enum is passed to APIs like File.Open to specify which mode to open the file with. Basically, this enum corresponds to Win32 constants passed to CreateFile. In fact, the values used for the different FileMode members directly map onto those used on the Win32 API being called. It’s clear that something like FileMode describes a set of discrete values that are mutually exclusive. You can only “open” the file in any of the six available modes. Combining things such as Append with ...

Get C# 4.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.