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:

Felicidade é uma coisa ótima. Tanto que, geralmente, nos sentimos plenos, inclusive, quando fazemos alguém feliz – não necessariamente a...
Se há algo que aprendi na “escola da vida” é que sempre que temos algo importante para decidir, se não...
Como você pode se considerar um profissional se você não sabe se todo seu código funciona? Como você pode saber...
Expressões regulares são fantásticas. Entretanto, precisam ser utilizadas com moderação pois podem impactar de forma perceptível a performance. A expressão...
In this post, let’s solve the “Euler Knight’s Tour” using F#. Disclaimer Sometimes life imposes a pause on us. I’m...
A ausência de padrões leves para externar (seja para documentação ou na elaboração) a arquitetura de um software sempre é...
× Precisa de ajuda?