Skip to content

Instantly share code, notes, and snippets.

@Scarjit
Created September 16, 2019 18:39
Show Gist options
  • Save Scarjit/6e2bd869ec734690a6822ccbcc2331f4 to your computer and use it in GitHub Desktop.
Save Scarjit/6e2bd869ec734690a6822ccbcc2331f4 to your computer and use it in GitHub Desktop.
Lazy evaluating datatype
public class LazyInit<T>
{
private T _val;
private bool _isIntialized;
private Func<T> lazy_lambda;
public LazyInit(Func<T> lazyLambda)
{
this.lazy_lambda = lazyLambda;
}
public T Value
{
get
{
if (!_isIntialized)
{
_val = lazy_lambda.Invoke();
_isIntialized = true;
return _val;
}
else
{
return _val;
}
}
set
{
if (_isIntialized)
{
return;
}
else
{
_val = value;
_isIntialized = true;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment