If I have the enum type:
Public enum Sport
{
Tennis = 0;
Football = 1;
Squash = 2;
Volleyball = 3;
}
Can I somehow add during run-time:
PingPong = 4
If I have the enum type:
Public enum Sport
{
Tennis = 0;
Football = 1;
Squash = 2;
Volleyball = 3;
}
Can I somehow add during run-time:
PingPong = 4
If the enum can be defined at program startup, you place the enum in a separate assembly and use a bootstrapper that will recompile the enum, overwriting the old version, and then launch the actual application. It's not the cleanest method, but it works.
Well yes you can create and or alter an Enum at runtime by using the EnumBuilder class, See the sample in MSDN
Having an enum value is, in my opinion, preferable over dictionaries or list based solution as one uses less memory and no thread side effects.
I use this when I need to generate data for Machine learning as having an Enum value and is preferable over lookup tables (in memory of database) as I just do not have the IO available with these large data sets.
Happy coding
Walter
I would create a complete Enum, with all the values you will ever need like this
And then keep a LIST of Invalid Sport entries, so the list would initially contain
PingPong
andRugby
. Every time you access the Enum, also check with your Invalid Sports list.Then you can simply adjust your Invalid Sports List to suit, at any stage
No, you cannot modify types at runtime. You could emit new types, but modifying existing ones is not possible.
The enum has a backing store, defaulting to int if you don't specify it. It is possible to directly assign values outside of the defined values:
Then you can check for it:
That is why you have the static function
Enum.IsDefined()
for checking if the actual value falls inside the expected values. Note that the function doesn't work for compound flag values.EDIT: After Hans Passant's comment: You don't have to use the literal value 4. You could use anything which returns an int. For example:
Here is more Object orientated way to maybe achieve what you are trying to achieve. This solution is inspired by early Java approach to enumeration:
To walk through different featues of this solution.
It's an immutable struct that wraps up an integer value. The value is enforced immutable by
readonly
keyword.The only way to create one of these structs is to call the constructor that takes the value as a parameter.
implicit operator int
is there so that the structure can be used in theswitch
bock - i.e. to make the structure convertible toint
.implicit operator Sport
is there so that you can assign integer values to the structure i.e.Sport rugby = 5
.const
values are the sports known at compile time. They can also be used ascase
labels.What I would actually do
This would create a value class
Sport
that has a name. Depending on your application you can extend this class to provide other attributes and methods. Having this as class gives you more flexibility as you can subclass it.Sports
class provides a static collection of sports that are known at compile time. This is similar to how some .NET frameworks handle named colors (i.e. WPF). Here is the usage:Once you do this, you'd find there is actuall very little need for having an enumeration as any conditional operations on the sport type can be handled within the
Sport
class.EDIT Realised that my equality operator will throw a
StackOverflowException
I always keep forgetting to writeObject.ReferenceEquals(obj,null)
instead ofobj==null
, which will recurse infinitely.