I suggest you ...

Querying types in a co- or contravariant way

The "in" and "out" keywords in generic type declarations are useful; however, due to their nature, their application is limited to a small number of types. What I am suggesting here, is to be able to use them in variable, parameter and possibly property declarations in order to restrict the set of possible operations on any generic type in a dynamic and temporary way. This would considerably increase the cases where co- and contravariance could be used.

Examples

// A List of a more derived type can be passed
public void ReadList(IList<out MyType> list)
{
MyType item = list[0]; // OK
list[0] = new MyType(); // DISALLOWED!
list.Add(new MyType()); // DISALLOWED!
}

// A List of a less derived type can be passed
public void WriteList(List<in MyType> list)
{
MyType item = list[0]; // DISALLOWED!
list[0] = new MyType(); // OK
list.Add(new MyType()); // OK
}

1 vote
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    Olivier J.Olivier J. shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    Thank you for your suggestion! While we aim to respond to every suggestion, we are closing older ones that don’t have enough votes so newer ones from you can move to the top. If this suggestion is still important to you, feel free to open it again.

    1 comment

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...

      Feedback and Knowledge Base