当接口ReadOnly属性在C#.NET中有效时,为什么不能在VB.NET中覆盖它? [英] Why can't Interface ReadOnly properties be overridden in VB.NET, when it is valid in C#.NET?
问题描述
(这与另一个问题)
如果您定义了一个接口,其中只有一个吸气剂的属性(在VB.NET中为ReadOnly), 为什么您可以在使用C#而不是VB来实现类中定义setter ?
If you define an Interface where there is a Property with only a getter (= ReadOnly in VB.NET), why can you define the setter in implementing classes with C# but not with VB ?
我以为它是在.NET级别定义的,
I would have thought it was defined at .NET level, and not language-specific.
示例:对于此界面
'VB.NET
Interface SomeInterface
'the interface only say that implementers must provide a value for reading
ReadOnly Property PublicProperty As String
End Interface
或
//C# code
interface IPublicProperty
{
string PublicProperty { get; }
}
这是C#中的正确实现:
This is a correct implementation in C# :
public class Implementer:IPublicProperty
{
private string _publicProperty;
public string PublicProperty
{
get
{
return _publicProperty;
}
set
{
_publicProperty = value;
}
}
}
但这在VB中无效.NET
But this is invalid in VB.NET
Public Property PublicProperty As String Implements SomeInterface.PublicProperty
Get
Return _myProperty
End Get
Set(ByVal value As String)
_myProperty = value
End Set
End Property
更新2015/04/23
事实证明,此功能是VB14的一部分!
参见语言C#6和VB 14 和 Visual Basic 14中的新语言功能中的功能:
Turns out this feature is coming as part of VB14 ! See Languages features in C# 6 and VB 14 and New Language Features in Visual Basic 14 :
ReadWrite道具可以实现ReadOnly接口属性
可以清除语言的古怪之处。看下面的示例:
ReadOnly interface properties can be implemented by ReadWrite props This cleans up a quirky corner of the language. Look at this example:
Interface I
ReadOnly Property P As Integer
End Interface
Class C : Implements I
Public Property P As Integer Implements I.P
End Class
以前,如果要实现ReadOnly属性IP,则
也必须使用ReadOnly属性实现。既然
的限制已经放宽了:您可以根据需要使用读/写
属性来实现它。这个例子碰巧是通过
读/写autoprop来实现的,但是您也可以使用带有getter和
setter的属性。
Previously, if you were implementing the ReadOnly property I.P, then you had to implement it with a ReadOnly property as well. Now that restriction has been relaxed: you can implement it with a read/write property if you want. This example happens to implement it with a read/write autoprop, but you can also use a property with getter and setter.
推荐答案
请小心地假设VB.NET和C#是同一语言,但口音不同。
Be careful assuming that VB.NET and C# are the same language spoken with a different accent - they're not.
因为VB.NET要求接口成员的实现必须具有 Implements
子句,从而说明要实现的成员。 C#使您可以显式(类似于VB.NET)或隐式(无VB.NET等效)实现接口成员。因此,此 actual C#版本是
Because VB.NET requires implementation of an interface member to have that Implements
clause, saying which member it is implementing. C# lets you implement interface members explicitly (SORT OF like VB.NET), or implicitly (no VB.NET equivalent). Therefore the actual C# version of this is
public class Implementer : IPublicProperty
{
private string _publicProperty;
string IPublicProperty.PublicProperty // explicit implementation
{
get
{
return _publicProperty;
}
set
{
_publicProperty = value;
}
}
}
给出错误:
and this does gives an error:
错误CS0550: ConsoleApplication171.Implementer.ConsoleApplication171.IPublicProperty.PublicProperty.set添加的访问器不是在接口成员'ConsoleApplication171.IPublicProperty.PublicProperty'中找到
error CS0550: 'ConsoleApplication171.Implementer.ConsoleApplication171.IPublicProperty.PublicProperty.set' adds an accessor not found in interface member 'ConsoleApplication171.IPublicProperty.PublicProperty'
这篇关于当接口ReadOnly属性在C#.NET中有效时,为什么不能在VB.NET中覆盖它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!