在C#中,是否可以使用具有不同名称的成员实现接口成员,就像在VB.NET中一样? [英] In C#, is it possible to implement an interface member using a member with a different name, like you can do in VB.NET?

查看:140
本文介绍了在C#中,是否可以使用具有不同名称的成员实现接口成员,就像在VB.NET中一样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这是我要问的一个问题,而不是在展示良好的编码实践(这实际上可能被认为是一种不好的做法),而是在可以的问题上完成。

Ok, this is a question I'm asking, not as in demonstrating good coding practices (this actually could be considered a bad practice) but rather in regards to 'can' it be done at all.

那就是说,在VB.NET中你实现了这样的界面......

That said, in VB.NET you implement an interface like this...

Sub SomeInterfaceMember()
    Implements ISomeInterface.SomeInterfaceMember
End Sub

在C#中你明确地这样做(只能通过界面调用)......

while in C# you do it explicitly like this (which can only be called via the interface)...

void ISomeInterface.SomeInterfaceMember(){}

或者更简单地,隐含地像这样(在这种情况下你可以直接调用它,或通过接口)...

or more simply, implicitly like this (in which case you can call it directly, or via the interface)...

void SomeInterfaceMember(){} // <-- This name matches the interface member

但是,关于VB,您也可以使用您想要的任何名称来执行此操作...

However, regarding VB, you can also do this, using any name you want for the member...

Sub SomeRandomMemberName() // <-- This name doesn't match the interface member
    Implements ISomeInterface.SomeInterfaceMember
End Sub

换句话说,处理实现的方法可以使用与接口成员名称完全不同的名称。

In other words, the method that handles the implementation can have a completely different name than the interface's member name.

I'我只是想知道在C#中是否有类似的东西。 (是的,我知道我可以简单地做一个显式接口,然后通过另一个'wrapper'成员使用一个简单的委托给它的不同名称来访问它,但是在VB.NET中,你只需要一个方法。)

I'm just wondering if there's something similar to this in C#. (Yes, I know I can simply do an explicit interface, then access it via another 'wrapper' member with a different name that simply delegates to it, but in VB.NET, you do both with a single method.)

那可以吗?

标记

推荐答案

不,这在C#中是不可能的。 (你不能在VB.NET中显式实现接口。)

No, this isn't possible in C#. (You can't "explicitly implement an interface" in VB.NET either.)

这篇关于在C#中,是否可以使用具有不同名称的成员实现接口成员,就像在VB.NET中一样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆