Domanda

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 .......;
});
È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top