Is there a chance legally?

This is the first time I asked a question, so help me in advance (and possibly others) in advance.

Today I was puzzled by covariance and contravariance in C # 4, but I really do not see the opportunity to do what I wanted. NOTE: all that should NOT solve a specific problem, but I would like to know if there is any legal way (brand-new co / contra-variance in C # 4) to solve this problem. So don't go crazy ...

Here is a sample code:

public interface IBase<in T>
{
    T GetValue();
    ...
}


public class Readable<T> : IBase<T>
{
    public T GetValue() {/* */}
    ...
}


public class Writeable<T> : IBase<T>
{
    public T GetValue() {/* */}
    public void SetValue(T value) {/* */}
    ...
}


public class Program
{
    private List<IBase<object>> _collection = new List<IBase<object>>();


    public static void Main()
    {
        var p = new Program();

        p._collection.Add(new Readable<bool>());    //not allowed
    }
}

IBase, T. , - , . - IBaseCore, IBase . : , IBase IBase?

.

+3
1

EDIT: -, IBase<T>. out T (), in T ().

, . , Readable<string>, Readable<bool>.

, :

public interface IBase
{
    object GetValueAsObject();
}

public interface IBase<out T> : IBase
{
    T GetValue();
    ...
}

List<IBase>.

+3

Source: https://habr.com/ru/post/1794456/


All Articles