Search code examples
c#interfacecovariancecontravariance

Can I implement an interface that contains a property that is of child type to what is required by the interface?


I am receiving the following error:

ClassName.PropertyName cannot implement IClassType.PropertyName because it does not have the matching return type of IBasePropertyType

Now, for the code:

public class ClassName : IClassType
{
    public IChildPropertyType PropertyName { get; set; }
}

public interface IClassType
{
    public IBasePropertyType PropertyName { get; set; }
}

public interface IBasePropertyType
{
    // some methods
}

public interface IChildPropertyType : IBasePropertyType
{
    // some methods
}

Is there a way to do what I am attempting? I know that the issue is with co/contravariance, but I can't seem to figure out how to do this.


Solution

  • In order to implement the given interface, you must have the same return type. However, there are a couple of potential work-arounds to make life easier:

    1. make your interface generic
    2. implement the interface explicitly.

    If you make IClassType generic, like so:

    public interface IClassType<T> where T : IBasePropertyType
    {
        public T PropertyName { get; set; }
    }
    

    ... then you can implement this interface using various property types:

    public class ClassName : IClassType<IChildPropertyType>
    {
        public IChildPropertyType PropertyName { get; set; }
    }
    

    Another option would be to leave your interface non-generic, but to have a generic base type that explicitly implements the interface:

    public class ClassBase<T> : IClassType
        where T : IChildPropertyType
    {
        IBasePropertyType IClassType.PropertyName { 
            get {return PropertyName;}
            set {PropertyName = (IChildPropertyType)value;}
        }
        T PropertyName {get;set;}
    }
    

    Note that this last option is not quite ideal because you must dynamically cast the property to the given child type: while you can guarantee that every IChildProperty type is an IBasePropertyType, you cannot guarantee that every IBasePropertyType is an IChildPropertyType. However, if you can eliminate the setter from the original interface, or if you can take other steps to guarantee that the setter will never be called with the wrong type in your code, then this could work.

    Update

    This doesn't fundamentally change the answer much, but C# now supports covariant and contravariant interfaces. For example, if you only expect your interface to expose the getter on your property, you could mark the type as an out type:

    public interface IClassType<out T>
    {
        public T PropertyName { get; }
    }
    

    Implementing types don't have to change anything--they can still implement the setter if they want to:

    public class ClassName : IClassType<IChildPropertyType>
    {
        public IChildPropertyType PropertyName { get; set; }
    }
    

    Then, even though your class implements IClassType<IChildPropertyType>, it can still be cast as an IClassType<IBasePropertyType> and used as an argument to any method written against that interface:

        IClassType<IBasePropertyType> c = new ClassName();
        IBasePropertyType prop = c.PropertyName;