Getting Enum without entering a class name?

I noticed that in C #, when I have Enum, say:

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

And when I need to access it, I have to go through this main class, and that is a lot. For instance,

 ClassObject.EventType.Click 

I hope that I can access it shorter, say: EventType.Click , will let me get this.

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

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

However, for this I get a syntax error.

Actually, creating another separate class just for this purpose is a bit troublesome, and I don’t even know if this is good practice or not. But ideally, I just want to shorten the name and probably avoid having to enter the class name before accessing the enumeration. Is it possible?

+4
source share
2 answers

You can put an enumeration at the same class depth that it should not be a member of the class

 namespace Test { public enum EventType { Click, Jump, Etc }; class ClassObject { } } 
+9
source

You define the scope of Enum in the class. That is why you must access it through the class. Define it outside the classroom and gain access from anywhere you want. Nested Enum and classes are not directly accessible.

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

Source: https://habr.com/ru/post/1440969/


All Articles