If I have an interface MyInterface1
interface MyInterface1
{
ImyGod myproperty { get; set; }
}
If I have a class do the following, it complains
class myClass : IMyInterface1
{
myGod myproperty { get; set; }
}
What should I iniatiate myClass with myGod here?
Thanks.
That’s just not possible. In order to implement an interface the signature of the member must be exactly the same as the signature defined in the interface. You will either need to change the interface or the class’s implementation so that the type of the property matches, exactly.
Since your property has a setter it means that, according to the interface, you would be able to set any object that implements
ImyGodto that property, but since the derived class is typed tomyGodinstead, it can’t support any other implementation ofImyGod.What you could do is use a generic interface, like so:
That will compile and work as intended, and also prevent someone setting a
someOtherGodinstance to that property which it clearly can’t support.