如何实现需要重复成员名称的接口?

发布于 2024-09-04 07:21:08 字数 1512 浏览 6 评论 0原文

我经常必须实现一些接口,例如 IEnumerable<我的代码中的 T>

每次,在自动实现时,我都会遇到以下情况:

public IEnumerator<T> GetEnumerator() {
    // Code here...
}

public IEnumerator GetEnumerator1() {
    // Code here...
}

虽然我必须同时实现 GetEnumerator() 方法,它们不可能具有相同的名称,即使我们知道它们以某种方式做相同的事情。编译器不能将它们视为一个是另一个的重载,因为只有返回类型不同。

这样做时,我设法将 GetEnumerator1() 访问器设置为 private。这样,编译器就不会抱怨未实现接口成员,我只需在方法体内抛出 NotImplementedException 即可。

然而,我想知道这是否是一个好的做法,或者我是否应该以不同的方式进行,例如方法别名或类似的东西。

实现诸如 IEnumerable 之类需要实现两个同名不同方法的接口时,最好的方法是什么?

编辑#1

在实现接口时,VB.NET 的反应是否与 C# 不同,因为在 VB.NET 中它是显式实现的,从而强制使用 GetEnumerator1()。代码如下:

Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of T) Implements System.Collections.Generic.IEnumerable(Of T).GetEnumerator
    // Code here...
End Function

Public Function GetEnumerator1() As System.Collections.Generic.IEnumerator Implements System.Collections.Generic.IEnumerable.GetEnumerator
    // Code here...
End Function

两个 GetEnumerator() 方法都是显式实现的,编译将拒绝它们具有相同的名称。为什么?

I often have to implement some interfaces such as IEnumerable<T> in my code.

Each time, when implementing automatically, I encounter the following:

public IEnumerator<T> GetEnumerator() {
    // Code here...
}

public IEnumerator GetEnumerator1() {
    // Code here...
}

Though I have to implement both GetEnumerator() methods, they impossibly can have the same name, even if we understand that they do the same, somehow. The compiler can't treat them as one being the overload of the other, because only the return type differs.

When doing so, I manage to set the GetEnumerator1() accessor to private. This way, the compiler doesn't complaint about not implementing the interface member, and I simply throw a NotImplementedException within the method's body.

However, I wonder whether it is a good practice, or if I shall proceed differently, as perhaps a method alias or something like so.

What is the best approach while implementing an interface such as IEnumerable<T> that requires the implementation of two different methods with the same name?

EDIT #1

Does VB.NET reacts differently from C# while implementing interfaces, since in VB.NET it is explicitly implemented, thus forcing the GetEnumerator1(). Here's the code:

Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of T) Implements System.Collections.Generic.IEnumerable(Of T).GetEnumerator
    // Code here...
End Function

Public Function GetEnumerator1() As System.Collections.Generic.IEnumerator Implements System.Collections.Generic.IEnumerable.GetEnumerator
    // Code here...
End Function

Both GetEnumerator() methods are explicitly implemented, and the compile will refuse them to have the same name. Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

过期情话 2024-09-11 07:21:09

您可以使用显式接口实现

IEnumerator IEnumerable.GetEnumerator()
{
    return GetEnumerator();
}

public IEnumerator<T> GetEnumerator()
{
    ...
}

You can use explicit interface implementation:

IEnumerator IEnumerable.GetEnumerator()
{
    return GetEnumerator();
}

public IEnumerator<T> GetEnumerator()
{
    ...
}
呆头 2024-09-11 07:21:09

在 Visual Basic 中,所有接口实现都是显式的

接口映射由 实现定义 语句,以便您可以随意命名接口实现方法。 (与 C# 不同,C# 中编译器通过匹配名称和签名来推断哪些方法实现了接口。)

更改方法名称和可见性(根据需要)是 VB 中的标准做法。有关详细概述,请参阅在 VB.NET 中实现接口

In Visual Basic, all interface implementations are explicit.

Interface mappings are defined by the Implements statement so you can name your interface implementation methods whatever you want. (Unlike C#, where the compiler infers which methods implement interfaces by matching their names and signatures.)

Changing the method name and visibility (as appropriate) is standard practice in VB. See Implementing Interfaces in VB.NET for a good overview.

从此见与不见 2024-09-11 07:21:09

您应该能够使用显式接口实现创建具有相同签名的两个方法。根据您所枚举的内容,我会将这些调用传递给内部 IEnumerable,例如列表或数组。

You should be able to use Explicit Interface Implementations to create the two methods that have the same signature. Depending on what you are enumerating, I would just pass these calls through to an internal IEnumerable<T> such as a List or array.

挽容 2024-09-11 07:21:09

显式实现非泛型接口允许两种方法具有相同的名称,并允许非泛型版本按照泛型版本来实现。沿着以下思路:

public class TestEnumerable : IEnumerable<int>
{
    public IEnumerator<int> GetEnumerator()
    {
        // Type-safe implementation here.
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

Implementing the non-generic interface explicitly allows both methods to have the same name, and allows the non-generic version to be implemented in terms of the generic one. Along the lines of:

public class TestEnumerable : IEnumerable<int>
{
    public IEnumerator<int> GetEnumerator()
    {
        // Type-safe implementation here.
    }

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