Pergunta

Is this the most laconic version of the code is show below? Is there any way to shorten it? Any syntactic sugar? Except var, I know about var.

Lazy<OrderEventItem[]> orderEventItems = new Lazy<OrderEventItem[]>(delegate
{
    return .......;
});
Foi útil?

Solução

var orderEventItems = new Lazy<OrderEventItem[]>(() => ...);

If you really want to get rid of the new Lazy<OrderEventItem[]>, you can create a generic static helper method:

static Lazy<T> CreateLazy<T>(Func<T> f)
{
    return new Lazy<T>(f);
}

and then your line becomes:

var orderEventItems = CreateLazy(() => ...);

Though, I find that less readable than the first approach.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top