Tuesday, December 03, 2013

Lazy(T) Class

in .NET 4 and newer:

Lazy(T) Class (System):

// The lazy initializer is created here, not the LargeObject
lazyLargeObject = new Lazy<LargeObject>(() => new LargeObject());
// LargeObject is not created yet here...
LargeObject large = lazyLargeObject.Value;
large.Data // only now LargeObject gets initialized


Lazy Initialization of an object means that its creation is deferred until it is first used.

No comments: