C# 接口中的枚举数据类型

发布于 2024-12-07 05:13:59 字数 310 浏览 2 评论 0原文

如何在接口中使用枚举数据类型?

这可能吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空城之時有危險 2024-12-14 05:13:59

enum 是 C# 中的保留关键字。如果你想使用它作为变量名,你可以在它前面加上 @ 前缀:

public enum Test { A, B, C };

public interface IParent1
{
    string method1(Test @enum);
}

public class Parent1 : IParent1
{
    public string method1(Test @enum)
    {
        return @enum.ToString();
    }
}

但我不喜欢使用保留字作为变量名。更好的方法是:

public enum Test { A, B, C };

public interface IParent1
{
    string method1(Test test);
}

public class Parent1 : IParent1
{
    public string method1(Test test)
    {
        return test.ToString();
    }
}

enum is a reserved keyword in C#. You can prefix it with @ if you want to use it as variable name:

public enum Test { A, B, C };

public interface IParent1
{
    string method1(Test @enum);
}

public class Parent1 : IParent1
{
    public string method1(Test @enum)
    {
        return @enum.ToString();
    }
}

But I don't like using reserved words for variable names. A better approach would be:

public enum Test { A, B, C };

public interface IParent1
{
    string method1(Test test);
}

public class Parent1 : IParent1
{
    public string method1(Test test)
    {
        return test.ToString();
    }
}
失去的东西太少 2024-12-14 05:13:59

不认为这有什么问题。但是为什么要将枚举声明嵌套在接口实现中呢?您的代码将无法编译,因为:
1.您正在使用保留字enum
2. value 未声明

尝试使用此:

public enum Test { A, B, C };

public interface IParent1 { string method1(Test @enum);}

public class Parent1 : IParent1
{
    public string method1(Test @enum)
    {
        return @enum.ToString();
    }
}

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 declared

Try use this:

public enum Test { A, B, C };

public interface IParent1 { string method1(Test @enum);}

public class Parent1 : IParent1
{
    public string method1(Test @enum)
    {
        return @enum.ToString();
    }
}
一百个冬季 2024-12-14 05:13:59

如果您希望每个实现类中的枚举都不同,您应该使用通用接口,例如“

public interface MyInterface<T>
{
    string MyMethod(T myEnum)
}

如果所有实现类都应该相同”,只是不要将其放在任何类之外:

public enum MyEnum { A, B, C }

public interface MyInterface
{
    string MyMethod(MyEnum myEnum)
}

If you want your enum to be different in each implementing class you should use a generic interface like

public interface MyInterface<T>
{
    string MyMethod(T myEnum)
}

If it should be the same for all implementing classes just don't put it outside any class:

public enum MyEnum { A, B, C }

public interface MyInterface
{
    string MyMethod(MyEnum myEnum)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文