Creating an empty strongly typed list of … an anonymous type (What?!)

Inspecting the code repository of a client, I found something like this:

var customer = new
{
    Id = default(int),
    Name = default(string)
};

var list = new int[0].Select(i => customer).ToList();

I confess that I needed some time to understand it. But, after some consideration, I figured out that what this code is doing is to create an empty strongly typed list of … an anonymous type.

Smart code. Yeah! But, please, don’t do that.

Compartilhe este insight:

6 respostas

  1. It’s clear for me that’s it’s bad thing from a design view, but what’s about the performance ?

  2. Should we change anonymous by nested private class and new int[0].Select(…) by Enumerable.Repeate(…, 0) ?
    Is ToList() (or ToArray) itself a problem as I already have a IEnumerable after Select ? ToList helps while debugging, should I remove all unecessary of them?

Deixe um comentário

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *

Elemar Júnior

Sou fundador e CEO da EximiaCo e atuo como tech trusted advisor ajudando diversas empresas a gerar mais resultados através da tecnologia.

Elemar Júnior

Sou fundador e CEO da EximiaCo e atuo como tech trusted advisor ajudando diversas empresas a gerar mais resultados através da tecnologia.

Mais insights para o seu negócio

Veja mais alguns estudos e reflexões que podem gerar alguns insights para o seu negócio:

Ontem, dia 25/07/2018, a convite do Canal.NET, compartilhei alguns insights sobre modelagem de microsserviços a partir de processos de negócio....
Publicado originalmente em meu linkedin Se há algo que aprendi, tanto academicamente quanto empiricamente, é que a motivação é intrínseca...
  Recently, I asked what would be the execution result of the following code: using System.Threading.Tasks; using static System.Console; class...
Um de meus temas de estudo preferidos no campo de desenvolvimento é design patterns. Isso sempre me levou a refletir...
In this post, I would like to explain a basic but confusing concept of CUDA programming: Thread Hierarchies. It will...
In the previous post, I asked which function, in the following code, would fill the array with 1’s faster and...
× Precisa de ajuda?