Created
September 16, 2019 18:39
-
-
Save Scarjit/6e2bd869ec734690a6822ccbcc2331f4 to your computer and use it in GitHub Desktop.
Lazy evaluating datatype
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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