Consider the following method,
public void Add(Enum key, object value);
Since Enum is a "special class", I didn't realize you could use the type in this way, but it compiles. Now, the .NET Framework Design Guidelines don't have anything to say about this construction, so I'm curious what everyone else thinks about this.
Here is what this method is doing internally:
public void Add(Enum key, object value)
{
this.dict.Add(key.ToString(), value)
}
Did you expect the parameter to be converted to a string or cast to an int?
Consider the following use-case,
public enum Names
{
John,
Joe,
Jill,
}
Add(Names.John, 10);
Add(Names.Joe, 11);
Is using enum in this way an acceptable alternative to using string constants (given that the domain is the set of all strings which are also valid enum identifiers)?
Last question: Does anyone know of any other real world scenarios that required passing the abstract Enum base class as parameter?
It's absolutely valid to use an enum value as a parameter.
Is using enum in this way an acceptable alternative to using string constants(...)?
Yes, in fact this is one of the main uses of enums.
Using a general Enum value is a little less obvious, since you generally want to know what values you can expect. A common use of enum values is to use it in a switch statement, and it helps to know what values can be expected. The name of an enum value is not generally that important, it's what it represents.
Yes, using Enum
as a parameter type is completely valid.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With