Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

283
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda