Reputation: 663
So I am trying to convert some VB code to C#. The property definition goes like this:
Public Overridable ReadOnly Property [Error]() As String Implements System.ComponentModel.IDataErrorInfo.Error
The conversion that I have seen makes it look like this:
public virtual string Error
Now I understand everything here except that it ignores the Implements line. Is this just not necessary in C#?
Upvotes: 2
Views: 183
Reputation: 6542
If you want to keep the property as readonly, then you need to use non-automatic properties (VB also forbids an automatic property implementing a readonly property):
Implicit implementation:
public string Error
{
get
{
//...
}
}
Explicit implementation:
string System.ComponentModel.IDataErrorInfo.Error
{
get
{
//...
}
}
Upvotes: 1
Reputation: 152566
Is this just not necessary in C#?
No - C# will implicitly implement an interface by default. Normally you can explicitly implement an interface method:
string System.ComponentModel.IDataErrorInfo.Error
but in your case the property is virtual
so you can't use explicit implementation (explicit implementations must be private
which makes no sense for virtual
members).
The main difference in implicit and explicit implementation is that a consumer will need to have a reference of the interface type, or cast the class to the interface:
MyDataErrorInfo impl = new MyDataErrorInfo();
string s;
IDataErrorInfo i = new MyDataErrorInfo(); // implicitly cast
s = i.Error; // works
s = ((IDataErrorInfo)impl).Error; // works
s = impl.Error; // does not work
Upvotes: 5
Reputation: 151604
A property implementation of an interface in C# doesn't need to mention the interface:
public virtual string Error { get; set; }
If you want an explicit interface implementation:
string IDataErrorInfo.Error { get; set; }
Upvotes: 1