How to access ENUM, which is declared in a separate class - C #

I have the following code:

class EmployeeFactory { public enum EmployeeType { ManagerType, ProgrammerType, DBAType } } 

I want to access this class MAIN (Program). I wrote the following code. IT IS WORKING. But I want to know how I can access ENUM without instantiating the class. Do ENUM tools look like a static variable (class level variable)? Any help?

 class Program { static void Main(string[] args) { Console.WriteLine(EmployeeFactory.EmployeeType.ProgrammerType); // WORKS WELL } } 

or do i need to write it that way?

 EmployeeFactory ef = new EmployeeFactory(); ef.EmployeeType.ProgrammerType 
+4
source share
3 answers

You can access it simply using the class.

 EmployeeFactory.EmployeeType.ProgrammerType 

Enumeration is part of the class, not part of the class instance.

+5
source

But I want to know how I can access ENUM without instantiating the class

The original way to access this listing

Console.WriteLine(EmployeeFactory.EmployeeType.ProgrammerType);

already doing this; you You get access to the enumeration without creating an instance of the class.

+1
source

try something like this ...

  public interface IEnums { public enum Mode { New, Selected }; } public class MyClass1 { public IEnums.Mode ModeProperty { get; set; } } public class MyClass2 { public MyClass2() { var myClass1 = new MyClass1(); //this will work myClass1.ModeProperty = IEnums.Mode.New; } } 

or you can directly access this ....

  EmployeeFactory.EmployeeType.ProgrammerType 

Hope this helps you.

-1
source

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


All Articles