VB.NET与C#中的属性实现的多级inheritance

假设我有2个接口定义如下:

public interface ISkuItem { public string SKU { get; set; } } public interface ICartItem : ISkuItem { public int Quantity { get; set; } public bool IsDiscountable { get; set; } } 

当我在C#中实现接口时,VS会生成以下模板化代码:

 public class CartItem : ICartItem { #region ICartItem Members public int Quantity { get {...} set {...} } public bool IsDiscountable { get {...} set {...} } #endregion #region ISkuItem Members public string SKU { get {...} set {...} } #endregion } 

在VB.NET中,同样的类是这样构建的:

 Public Class CartItem Implements ICartItem Public Property IsDiscountable As Boolean Implements ICartItem.IsDiscountable 'GET SET' End Property Public Property Quantity As Integer Implements ICartItem.Quantity 'GET SET' End Property Public Property SKU As String Implements ISkuItem.SKU 'GET SET' End Property End Class 

VB.NET明确要求您在每个Implements IInterfaceName.PropertyName之后添加Implements IInterfaceName.PropertyName ,而C#只使用region s来指示哪些属性和方法属于该接口。

有趣的是在VB.NET中,在SKU属性上,我可以指定Implements ISkuItem.SKUImplements ICartItem.SKU 。 虽然VS构建的模板默认为ISkuItem ,但如果需要,我也可以指定ICartItem 。 奇怪的是,因为C#只使用region来阻止inheritance的属性,所以我似乎无法在C#中明确指定SKU的实现接口,就像我在VB.NET中那样。

我的问题是: 能否指定一个或另一个接口来实现VB.NET中的属性是否有任何重要性,如果是这样,有没有办法在C#中模仿这个function? 此外,在实现属性时,将一个接口指定给另一个接口会产生什么影响?

我认为其他答案实际上有点不合适。

在您发布的示例中,一个接口inheritance自另一个接口。 这仅仅意味着它提供与其基础相同的成员,以及一些其他成员。

这些不是两个独立的接口,恰好暴露了具有相同名称的成员。 ICartItem.SKU ISkuItem.SKU 相同ICartIteminheritance自ISkuItem只意味着ISkuItem作为接口代表ICartItem提供的function的子集。

考虑以下代码:

 class CartItem : ICartItem { public int Quantity { get; set; } public bool IsDiscountable { get; set; } string ISkuItem.SKU { get { return "ISkuItem"; } set { throw new NotSupportedException(); } } string ICartItem.SKU { get { return "ICartItem"; } set { throw new NotSupportedException(); } } } 

这个类不会编译 。 在这种情况下,您无法显式定义ICartItem.SKU ,因为ICartItem.SKU只是ISkuItem.SKU 。 没有“其他” SKU属性可供定义。

所以,直接回答你的问题:

在VB.NET中能够指定一个或另一个接口来实现适当的特性是否有任何重要性?

当它们是分开的,不相关的接口时是的
正如其他人所指出的,您可以为共享通用名称的不同接口成员提供不同的实现。

但是当一个接口inheritance另一个接口时没有
没关系,因为它们是一回事。

在实现适当的时候,指定一个接口而不是另一个接口有什么影响?

同样,如果它们是不相关的接口,它具有其他人已经讨论过的效果:为这两个接口提供不同的实现。 但如果一个来自另一个,它就没有效果

是的,这很重要,这称为Explicit和Implicit接口实现。

在C#中,您可以通过在方法名前加上接口名称来实现此目的,如下所示:

 public class CartItem : ICartItem, ISkuItem { #region ICartItem Members public int Quantity { get {...} set {...} } public bool IsDiscountable { get {...} set {...} } #endregion #region ISkuItem Members public string ISkuItem.SKU { get {...} set {...} } //like this public string ICartItem.SKU { get {...} set {...} } //like this #endregion } 

是的,您可以在每个界面后面实现不同的function。 假设两个接口具有相同的签名。 根据您实现的实现接口,将控制执行哪个接口。

… C#explict接口示例…

 public interface ITest1 { string Get(); } public interface ITest2 { string Get(); } // new is just to get rid of a compiler warning public interface ITest3 : ITest1, ITest2 { new string Get(); } public class MyTest : ITest1, ITest2 { public string Get() { return "local"; } string ITest1.Get() { return "hello"; } string ITest2.Get() { return "world"; } string ITest3.Get() { return "hi"; } } class Program { static void Main(string[] args) { var mytest = new MyTest(); // note that if mytest.Get() does not exist if all of the // interfaces are explicit var v0 = mytest.Get(); //local var v1 = ((ITest1)mytest).Get(); //hello var v2 = ((ITest2)mytest).Get(); //world var v3 = ((ITest3)mytest).Get(); //hi } } 

… VB.Net中的类似代码…

 Module Module1 Sub Main() Dim myinstance = New MyTest() Dim v0 = myinstance.DoWork() 'local 'By the way... note that the following methods are called 'by the interface signature and not the defind method name 'in the class Dim v1 = DirectCast(myinstance, ITest1).DoWork() 'hello Dim v2 = DirectCast(myinstance, ITest2).DoWork() 'world Dim v3 = DirectCast(myinstance, ITest3).DoWork() 'hi End Sub End Module Public Interface ITest1 Function DoWork() As String End Interface Public Interface ITest2 Function DoWork() As String End Interface Public Interface ITest3 Inherits ITest1 Inherits ITest2 Shadows Function DoWork() As String End Interface Public Class MyTest Implements ITest3 'Implements ITest1 'Implements ITest2 Public Function DoWork() As String Return "local" End Function Private Function DoWork1() As String Implements ITest1.DoWork Return "hello" End Function Private Function DoWork2() As String Implements ITest2.DoWork Return "world" End Function Private Function DoWork3() As String Implements ITest3.DoWork Return "hi" End Function End Class