C# 接口中的枚举数据类型
如何在接口中使用枚举数据类型?
这可能吗?
public interface IParent1
{
string method1(Test enum);
}
public class Parent1 : IParent1
{
public enum Test
{
A,
B,
C
}
public string method1(Test enum)
{
return enum.ToString();
}
}
How to use enum datatype in interface?
Is this possible?
public interface IParent1
{
string method1(Test enum);
}
public class Parent1 : IParent1
{
public enum Test
{
A,
B,
C
}
public string method1(Test enum)
{
return enum.ToString();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
enum
是 C# 中的保留关键字。如果你想使用它作为变量名,你可以在它前面加上@
前缀:但我不喜欢使用保留字作为变量名。更好的方法是:
enum
is a reserved keyword in C#. You can prefix it with@
if you want to use it as variable name:But I don't like using reserved words for variable names. A better approach would be:
不认为这有什么问题。但是为什么要将枚举声明嵌套在接口实现中呢?您的代码将无法编译,因为:
1.您正在使用保留字
enum
2.
value
未声明尝试使用此:
Don't see any problem with that. But why do you nest your enum declaration in the interface implementation? Your code will not compile, because:
1. You're using reserved word
enum
2.
value
is not declaredTry use this:
如果您希望每个实现类中的枚举都不同,您应该使用通用接口,例如“
如果所有实现类都应该相同”,只是不要将其放在任何类之外:
If you want your enum to be different in each implementing class you should use a generic interface like
If it should be the same for all implementing classes just don't put it outside any class: