Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it acceptable to design a method with a parameter of type System.Enum? [closed]

Tags:

c#

enums

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?

like image 507
Dave Avatar asked Dec 22 '22 09:12

Dave


2 Answers

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.

like image 80
Rik Avatar answered May 12 '23 00:05

Rik


Yes, using Enum as a parameter type is completely valid.

like image 37
Taylor Leese Avatar answered May 12 '23 01:05

Taylor Leese