(这是关系到this另一个问题)
如果你定义一个接口,其中有一个楼盘,只有一个吸气剂(=只读在VB.NET),为什么你能在实现类与C#而不是用VB ?定义二传手
我还以为它被定义在.NET的水平,而不是特定语言。
例:此接口
VB.NET
接口SomeInterface
界面只能说是实施者必须提供阅读价值
只读属性PublicProperty作为字符串
结束接口
或
// C#code
接口IPublicProperty
{
字符串PublicProperty {获得; }
}
这是在C#中正确执行:
公共类实施者:IPublicProperty
{
私人字符串_publicProperty;
公共字符串PublicProperty
{
得到
{
返回_publicProperty;
}
组
{
_publicProperty =价值;
}
}
}
但是,这是无效的VB.NET
公共财产PublicProperty作为字符串实现SomeInterface.PublicProperty
得到
返回_myProperty
最终获取
设置(BYVAL值作为字符串)
_myProperty =价值
结束设定
高端物业
更新2015年4月23日
原来,这个功能是未来的VB14的一部分! 请参见语言功能在C#6 VB 14 和新的语言功能在Visual Basic 14 :
只读接口性质可以通过读写道具来实现 这种清理语言的一个古怪的角落。请看下面的例子:
接口I
只读属性P作为整数
结束接口
C类:实现我
公共属性P作为整数器具腹膜内
末级
previously,如果你正在实施的只读属性IP,然后 你有没有和只读属性来实现它。现在 限制已放宽:你可以用一个读/写实现它 属性,如果你想。这个例子发生在一个实现它 读/写autoprop,但你也可以使用属性与getter和 二传手。
解决方案注意假定VB.NET和C#是相同的语言,有不同的口音 - 他们不是
由于VB.NET需要实现一个接口成员有这种器具
条款,称该成员被执行。 C#中,您可以明确地实现接口成员(有点像VB.NET),或隐式(无VB.NET当量)。因此,实际的C#版本,这是
公共类实施者:IPublicProperty
{
私人字符串_publicProperty;
字符串IPublicProperty.PublicProperty //明确实施
{
得到
{
返回_publicProperty;
}
组
{
_publicProperty =价值;
}
}
}
和本的没有的给出了一个错误:
错误CS0550:ConsoleApplication171.Implementer.ConsoleApplication171.IPublicProperty.PublicProperty.set增加了在接口成员中没有发现的访问ConsoleApplication171.IPublicProperty.PublicProperty
(this is related to this other question )
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 ?
I would have thought it was defined at .NET level, and not language-specific.
Example : for this interface
'VB.NET
Interface SomeInterface
'the interface only say that implementers must provide a value for reading
ReadOnly Property PublicProperty As String
End Interface
or
//C# code
interface IPublicProperty
{
string PublicProperty { get; }
}
This is a correct implementation in C# :
public class Implementer:IPublicProperty
{
private string _publicProperty;
public string PublicProperty
{
get
{
return _publicProperty;
}
set
{
_publicProperty = value;
}
}
}
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
UPDATE 2015/04/23
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 :
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
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.
解决方案
Be careful assuming that VB.NET and C# are the same language spoken with a different accent - they're not.
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:
error CS0550: 'ConsoleApplication171.Implementer.ConsoleApplication171.IPublicProperty.PublicProperty.set' adds an accessor not found in interface member 'ConsoleApplication171.IPublicProperty.PublicProperty'