Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is an implementation that I use.</p> <pre><code>/// &lt;summary&gt; /// Provides support for lazy initialization. /// &lt;/summary&gt; /// &lt;typeparam name="T"&gt;Specifies the type of object that is being lazily initialized.&lt;/typeparam&gt; public sealed class Lazy&lt;T&gt; { private readonly object padlock = new object(); private readonly Func&lt;T&gt; createValue; private bool isValueCreated; private T value; /// &lt;summary&gt; /// Gets the lazily initialized value of the current Lazy{T} instance. /// &lt;/summary&gt; public T Value { get { if (!isValueCreated) { lock (padlock) { if (!isValueCreated) { value = createValue(); isValueCreated = true; } } } return value; } } /// &lt;summary&gt; /// Gets a value that indicates whether a value has been created for this Lazy{T} instance. /// &lt;/summary&gt; public bool IsValueCreated { get { lock (padlock) { return isValueCreated; } } } /// &lt;summary&gt; /// Initializes a new instance of the Lazy{T} class. /// &lt;/summary&gt; /// &lt;param name="createValue"&gt;The delegate that produces the value when it is needed.&lt;/param&gt; public Lazy(Func&lt;T&gt; createValue) { if (createValue == null) throw new ArgumentNullException("createValue"); this.createValue = createValue; } /// &lt;summary&gt; /// Creates and returns a string representation of the Lazy{T}.Value. /// &lt;/summary&gt; /// &lt;returns&gt;The string representation of the Lazy{T}.Value property.&lt;/returns&gt; public override string ToString() { return Value.ToString(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload