Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What you want is <em>call site covariance</em>, which is not a feature that C# supports. C# 4 and above support generic variance, but not call site variance.</p> <p>However, that doesn't help you here. You want a dog groomer to be put in a list of animal groomers, but that can't work in C#. A dog groomer cannot be used in any context in which an animal groomer is needed because <strong>a dog groomer can only groom dogs</strong> but an animal groomer can also groom cats. That is, you want the interface to be <em>covariant</em> when it cannot be safely used in a covariant manner. </p> <p>However your <code>IAnimalGroomer&lt;T&gt;</code> interface could be <em>contravariant</em> as it stands: an animal groomer can be used in a context in which a dog groomer is required, because an animal groomer can groom dogs. If you made <code>IAnimalGroomer&lt;T&gt;</code> contravariant by adding <code>in</code> to the declaration of <code>T</code> then you could put an <code>IAnimalGroomer&lt;IAnimal&gt;</code> into an <code>IList&lt;IAnimalGroomer&lt;Dog&gt;&gt;</code>.</p> <p>For a more realistic example, think of <code>IEnumerable&lt;T&gt;</code> vs <code>IComparer&lt;T&gt;</code>. A sequence of dogs may be used as a sequence of animals; <code>IEnumerable&lt;T&gt;</code> is <em>covariant</em>. But a sequence of animals may not be used as a sequence of dogs; there could be a tiger in there. </p> <p>By contrast, a comparer that compares animals may be used as a comparer of dogs; <code>IComparer&lt;T&gt;</code> is <em>contravariant</em>. But a comparer of dogs may not be used to compare animals; someone could try to compare two cats.</p> <p>If that is still not clear then start by reading the FAQ:</p> <p><a href="http://blogs.msdn.com/b/csharpfaq/archive/2010/02/16/covariance-and-contravariance-faq.aspx" rel="noreferrer">http://blogs.msdn.com/b/csharpfaq/archive/2010/02/16/covariance-and-contravariance-faq.aspx</a></p> <p>and then come back and ask more questions if you have them.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload