vb.net C#和VB.NET中的接口实现

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16105040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:19:13  来源:igfitidea点击:

Interface implementation in C# and VB.NET

c#vb.netinterface

提问by Brij

I have an interface defined in C#project:

我在C#项目中定义了一个接口:

public interface IForm
{
    bool IsDisposed { get; }
    void Show();
}

I implemented it in a C#class in WinForms

C#在 WinForms 的一个类中实现了它

public partial class Form1 : Form, IForm {  }

As the method Show()and property IsDisposedare already present in the System.Windows.Forms.Formclass I do not require to implement it.

由于方法Show()和属性IsDisposed已经存在于System.Windows.Forms.Form类中,我不需要实现它。

But same doesn't hold true in VB.NET, in VB.NETI have to define the members, otherwise getting compile error.

但同样在VB.NETVB.NET我必须定义成员,否则会出现编译错误。

Public Class Form1
    Implements WindowsFormsApplication1.IForm

    Public ReadOnly Property IsDisposed1 As Boolean Implements WindowsFormsApplication1.IForm.IsDisposed
        Get

        End Get
    End Property

    Public Sub Show1() Implements WindowsFormsApplication1.IForm.Show

    End Sub
End Class

Why do I have to define the interface members in VB.NET if they are already present in the base class ?

为什么我必须在 VB.NET 中定义接口成员,如果它们已经存在于基类中?

回答by Patrik H?gne

VB does not have implicit interface implementation, only explicit while C# supports both.

VB 没有隐式接口实现,只有显式实现,而 C# 两者都支持。

This means that you have to explicitly say exactly what member that implements an interface member. This adds some flexibility, for example you can make the method that implements an interface member private or protected and you can give it a name that differs from the interface member.

这意味着您必须明确说明实现接口成员的成员。这增加了一些灵活性,例如您可以将实现接口成员的方法设为私有或受保护,并且您可以为其指定一个与接口成员不同的名称。

You can read more about the details of this here: http://ondevelopment.blogspot.se/2008/10/implementing-interfaces-in-vbnet.html

您可以在此处阅读有关详细信息的更多信息:http: //ondevelopment.blogspot.se/2008/10/implementing-interfaces-in-vbnet.html

回答by jbabey

VB.NET allows you to name a function/sub differently than the function/sub that it implements. This means that you must explicitly add the Implements <Function/Sub>to the end of the signature.

VB.NET 允许您以不同于它实现的函数/子的方式命名函数/子。这意味着您必须将 显式添加Implements <Function/Sub>到签名的末尾。

In C# you can't do this, so the implementations "just work" without you having to add anything.

在 C# 中,您不能这样做,因此实现“可以正常工作”而无需添加任何内容。

MSDN:

微软

The parameter types and return types of the implementing member must match the interface property or member declaration in the interface. The most common way to implement an element of an interface is with a member that has the same name as the interface

实现成员的参数类型和返回类型必须与接口中的接口属性或成员声明相匹配。实现接口元素的最常见方法是使用与接口同名的成员

回答by Kuffs

Use the shadows keyword if you want to override the standard methods of the Form and replace them with the ones defined in your interface otherwise you are required to use a different name as they are treated as two separate methods.

如果您想覆盖 Form 的标准方法并将它们替换为您的接口中定义的方法,请使用 shadows 关键字,否则您需要使用不同的名称,因为它们被视为两个单独的方法。

Public Class Form1
    Inherits Form
    Implements IForm

    Public Shadows Property IsDisposed As Boolean Implements IForm.IsDisposed

    Public Shadows Sub Show() Implements IForm.Show
        ' replaces original method in Form class
    End Sub

End Class

Alternative:

选择:

Public Class Form2
    Inherits Form
    Implements IForm

    Public Property IsDisposed1 As Boolean Implements IForm.IsDisposed

    Public Sub Show1() Implements IForm.Show
        Me.Show() ' Original method still exists and is accessible like this
    End Sub
End Class