如何访问在单独的类中声明的 ENUM - C#
我有以下代码:
class EmployeeFactory
{
public enum EmployeeType
{
ManagerType,
ProgrammerType,
DBAType
}
}
我想在 MAIN 类(程序)中访问它。我编写了以下代码。有用。但我想知道如何在不实例化类的情况下访问 ENUM - 意味着 ENUM 就像一个静态变量(类级变量)?有什么帮助吗?
class Program
{
static void Main(string[] args)
{
Console.WriteLine(EmployeeFactory.EmployeeType.ProgrammerType); // WORKS WELL
}
}
或者我需要这样写吗?
EmployeeFactory ef = new EmployeeFactory();
ef.EmployeeType.ProgrammerType
I have the following code:
class EmployeeFactory
{
public enum EmployeeType
{
ManagerType,
ProgrammerType,
DBAType
}
}
I want to access this in MAIN class (Program). I have written the following code. IT WORKS. But I want to know how I can access the ENUM without instantiating the class -- Means ENUM is 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 this way?
EmployeeFactory ef = new EmployeeFactory();
ef.EmployeeType.ProgrammerType
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您只需使用该类即可访问它。
枚举是类的一部分,而不是类实例的一部分。
You can access it simply using the class.
The enum is part of the class, not part of a class instance.
访问此枚举的原始方式
Console.WriteLine(EmployeeFactory.EmployeeType.ProgrammerType);
已经实现了这一点;您正在访问枚举而不实例化该类。
The original way you're accessing this enum
Console.WriteLine(EmployeeFactory.EmployeeType.ProgrammerType);
already accomplishes that; you are accessing the enum without instantiating the class.
尝试这样的事情......
或者
你可以像这样直接访问......
我希望它能帮助你
try something like this ...
or
you can directly access like this....
i hope it will helps you