Search code examples
c#enumssetvalue

C# Change int value of item in enum


I have this enum

 enum Items { pen = 5, watch = 4, rubber = 1, ruler = 8};

and I want to change the value of watch (now is 4) to 6. So something like this:

Items.watch = 6;

but this doesn't work. Any ideas?


Solution

  • The value of an enum is fixed at compile time. According to Microsoft's C# documentation:

    The enum keyword is used to declare an enumeration, a distinct type that consists of a set of named constants called the enumerator list.

    Note the term "constants". If you want to change the integer value of an enum entry, you must recompile the code.