2.11. The enum Value Type

Often when we program we need to define a set of alternative attributes to associate with an object. A file, for example, might be open in one of three states: input, output, and append. One strategy to keep track of these state values is to associate a unique constant number with each one. Thus we might write

public class fileMode
{
      public const int input  = 1;
      public const int output = 2;
      public const int append = 3;
      // ...
}

and use these constants as follows:

bool open_file( string file_name, int open_mode);
// ...
open_file( "Phoenix_and_the_Crane", fileMode.append );

The benefit is that it is much easier to use a set of mnemonic constant objects than to remember and make sense of the associated literal values. ...

Get C# Primer: A Practical Approach 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.