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

343
Vistas
Why use a generic type and then limit it to one type with "where"?

What's the advantage of using this:

DataTable CopyToDataTable<T>(this IEnumerable<T> source) where T : DataRow

where you can just do this:

DataTable CopyToDataTable<T>(this DataRow[] source)

Excuse me if this is a dumb question, I'm new at this

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

I think there are two parts to this.

The first is that accepting an IEnumerable is more flexible than accepting an array. What if the user has a List<DataRow>, or some other collection type? What if they want to pass the result of a linq query? Accepting an array forces them to allocate a new array just to pass to CopyToDataTable, and that's unnecessary cost.

There's no reason why CopyToDataTable needs an array: it just needs an IEnumerable. So it's best if it just accepts an IEnumerable.

That said, due to covariance it would be possible to use the signature:

DataTable CopyToDataTable(this IEnumerable<DataRow> source)

... and users would be able to pass e.g. an IEnumerable<TableDetailsRow> (where TableDetailsRow extends DataRow). However, covariance was only introduced in C# 4, and that method has been around since .NET 3.5, which means it was probably written in C# 3. Since covariance wasn't available then, generics was the next best thing.

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