Skip to content

Instantly share code, notes, and snippets.

@tmyt
Created October 24, 2015 20:00
Show Gist options
  • Save tmyt/86505f224e8ef4859a06 to your computer and use it in GitHub Desktop.
Save tmyt/86505f224e8ef4859a06 to your computer and use it in GitHub Desktop.
public class CachedValue<T>
{
private bool _hasValue;
private T _value;
private Func<T> _getter;
public T Value => _hasValue ? _value : (_value = _getter());
public CachedValue(Func<T> getter)
{
_getter = getter;
}
public static implicit operator CachedValue<T>(Func<T> getter)
{
return new CachedValue<T>(getter);
}
}
public class Hoge
{
private CachedValue<bool> Is64Bit =>
new Func<bool>(() => Marshal.SizeOf<IntPtr>() == 8);
// こう書きたい人生だった
//private CachedValue<bool> Is64Bit =>
// () => Marshal.SizeOf<IntPtr>() == 8;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment