Reputation: 188
If my interface has the signature only for getter such as:
public interface IInterface
{
object Id{get;}
}
So the interface only dictates a public getter for Id on any implemented class now when i have the class :
public class Simple : IInterface
{
object Id
{
get{return something;}
set{ do something else;}
}
}
the compiler complains about the setter as the setter is not defined in the interface. However I didnt dictate anything on the interface contract for a setter; why does the interface insist on the setter on the derived classes ?
Upvotes: 2
Views: 764
Reputation: 81159
In designing .net, Microsoft decided to make there be three non-interchangeable types of properties: read-only, write-only, and read-write. In C#, if one declares a read-write property with the same name as one or more interface properties one is supposed to implement, the compiler can automatically create not only the read-write property the programmer actually specified, but read-only and/or write-only properties as needed to satisfy the interfaces. For example, if interface IReadableFoo implements a read-only property Foo, IWritableFoo implements a write-only property Foo, and IReadWriteFoo inherits IReadableFoo and IWritablefoo, and implements a "new" read-write property Foo, and a class ReadWriteFoo implements IReadWriteFoo and declares a public read-write property Foo, the compiler will have ReadWriteFoo generate interface implementations of read-only property IReadableFoo.Foo, write-only property IWritableFoo.Foo, and read-write property IReadWriteFoo.Foo.
Upvotes: 1
Reputation: 564413
You just need to make Id public. For example, this compiles fine:
public interface IInterface
{
object Id { get; }
}
public class Simple : IInterface
{
private int something;
public object Id
{
get { return something; }
set{ something = (int)value;}
}
}
Upvotes: 11