O'Reilly logo

C# Primer: A Practical Approach by Stanley B. Lippman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required