Flags
Besides their use for discrete named constants, you can also use enums to describe flags. For example, System.IO
defines the FileOpen
enum as follows:
public enum FileMode{ CreateNew = 1, Create = 2, Open = 3, OpenOrCreate = 4, Truncate = 5, Append = 6,}
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 ...
Get C# 5.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.