(这与其他问题有关)
如果你定义一个接口,其中有一个属性,只有一个getter(=只读在VB.NET),为什么你能在实施与C#类定义的制定者而不是用VB?
我原以为它是在.NET级别定义的,而不是特定于语言的.
示例:对于此接口
'VB.NET
Interface SomeInterface
'the interface only say that implementers must provide a value for reading
ReadOnly Property PublicProperty As String
End Interface
Run Code Online (Sandbox Code Playgroud)
要么
//C# code
interface IPublicProperty
{
string PublicProperty { get; }
}
Run Code Online (Sandbox Code Playgroud)
这是C#中的正确实现:
public class Implementer:IPublicProperty
{
private string _publicProperty;
public string PublicProperty
{
get
{
return _publicProperty;
}
set
{
_publicProperty = value;
}
}
}
Run Code Online (Sandbox Code Playgroud)
但这在VB.NET中无效
Public Property PublicProperty As String Implements SomeInterface.PublicProperty
Get
Return _myProperty
End Get …Run Code Online (Sandbox Code Playgroud) 以下适用于C#:
interface I
{
int X { get; }
}
class C : I
{
public int X
{
get { ... }
set { ... }
}
}
Run Code Online (Sandbox Code Playgroud)
以下在VB.NET中不起作用:
Interface I
ReadOnly Property X As Integer
End Interface
Class C
Implements I
Public Property X As Integer Implements I.X
Get
...
End Get
Set(value As Integer)
...
End Set
End Property
End Class
Run Code Online (Sandbox Code Playgroud)
错误信息Implementing property must have matching 'ReadOnly' or 'WriteOnly' specifiers非常明显,所以我知道这里有什么问题.这也不是一个大问题,因为解决这个限制很容易.
不过我很好奇:有谁知道为什么VB设计师决定以不同于C#的方式处理这种情况?