C# – Setting enum value at runtime in C#

c++enumsruntime

Is there any way that I can change enum values at run-time?

e.g I have following type

enum MyType
{
   TypeOne, //=5 at runtime 
   TypeTwo  //=3 at runtime
}

I want at runtime set 5 to TypeOne and 3 to TypeTwo.

Best Solution

As others have pointed out, the answer is no.

You could however probably refactor your code to use a class instead:

public sealed class MyType
{
   public int TypeOne { get; set; }
   public int TypeTwo { get; set; }
}

...

var myType = new MyType { TypeOne  = 5, TypeTwo = 3 };

or variations on that theme.