Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

275
Views
C# Covariance / Contravariance in IEnumerable<T> where T is generic type, interface vs class

does someone understand why having a generic where constraint as class is different than interface? This code does not compile:

public interface IInterface
{
}

public class Class<T> where T : IInterface
{
    public void Do()
    {
        IEnumerable<IInterface> ret = GetEnumerable();
    }

    public IEnumerable<T> GetEnumerable()
    {
        return new T[0];
    }
}

whereas, by changing IInterface to class, suddenly compiles

public class IInterface
{
}

public class Class<T> where T : IInterface
{
    public void Do()
    {
        IEnumerable<IInterface> ret = GetEnumerable();
    }

    public IEnumerable<T> GetEnumerable()
    {
        return new T[0];
    }
}

I know that I can use:

IEnumerable<IInterface> ret = (IEnumerable<IInterface>)GetEnumerable();

but I really would like to know the reason why is the cast necessary if IInterface is interface.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

The real question is, is it really necessary?

T, in this specific case, should always be a class.

You can ensure that by additionally adding the class constraint.

public interface IInterface
{
    bool Foo { get; set; }
}

public class Class<T> where T : class, IInterface
{
    public void Do()
    {
        var ret = GetEnumerable();

        foreach (var item in ret)
        {
            item.Foo = true; // you can handle the object just if you would have an object of that type.
        }
    }

    public IEnumerable<T> GetEnumerable()
    {
        return new T[0];
    }
}

Now if you remove the class constraint it would still work. However, if you hover over the squigglies of that item.Foo = true it will come apparent why:

enter image description here

over 4 years ago · Santiago Trujillo Report

0

As stated in the documentation:

Variance applies only to reference types; if you specify a value type for a variant type parameter, that type parameter is invariant for the resulting constructed type.

With your interface example, there is no such constraint on T, whereas with the class example this constraint is inferred.

If you need to constrain to an interface, an additional "reference type constraint" is required:

public class Class<T> where T : class, IInterface
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!