Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the Enum without having to type its class name?

Tags:

c#

enums

I notice that in C#, when I have an Enum, say:

class ClassObject {
   public static Enum EventType = {Click, Jump, Etc};
}

And when I have to access it, I have to go through it's main class and it is very verbose. For eg:

ClassObject.EventType.Click

I hope that I could access it in a shorter manner, say: EventType.Click, will allow me get that.

So what I thought I could try to create a separate class and extend it from Enum:

class EventType : Enum {
   Click, Jump, Etc;
}

However, this I'm getting a syntax error for this.

Actually, creating another separate class just for this purpose is a little troublesome and I don't even know if this is a good practice or not. But ideally, I just wish to shorten the name and probably avoid the need for having to type the class name before accessing the enum. Is this possible?

like image 520
Carven Avatar asked Dec 12 '22 21:12

Carven


1 Answers

You can put the enum at the same depth of the class, it doesn't have to be a class member

namespace Test
{
    public enum EventType
    {
        Click,
        Jump,
        Etc
    };

    class ClassObject { }
}
like image 140
hunter Avatar answered Jan 14 '23 09:01

hunter