how to to check generic enum var

essamce 621 Reputation points
2023-07-24T11:16:00.92+00:00
///////////////// enum def ///////////////////////////////////
    public enum MyEnum1 { m1, m2, m3, ...  ,Help };
    public enum MyEnum2 { m1, m2, m3, ...  ,Help };
    public enum MyEnum3 { m1, m2, m3, ...  ,Help };
                   .
                   .
                   .
    public enum MyEnum99 { m1, m2, m3, ...  ,Help };

/////////////// use case /////////////////////////////////////
   void foo<TEnum>( TEnum x) where TEnum : struct
   {
	// TO DO
	if ( x == Help ) // error
		// do stuf with TEnum
////////////////////////////////////////////////////////////////

i have many enums most of them have element name "Help",

is there a way to check if a generic var x is "Help" without hard coding casting all 99+ enum ?

thanks in advance, any idea will be appreciated.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,090 questions
{count} votes

Accepted answer
  1. Viorel 119.6K Reputation points
    2023-07-24T11:40:03.15+00:00

    For example:

    if( Enum.GetName( typeof( TEnum ), x ) == "Help" )
    {
        . . .
    }
    
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.