-
-
Save kou-yeung/733479a9ee0609b918d2d7dd1b6a922e to your computer and use it in GitHub Desktop.
.NET4.0に追加された Lazy<T> ( 遅延初期化 ) クラスをUnityで使いたいので適当に書いた。
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
namespace System.Ex | |
{ | |
public class Lazy<T> | |
{ | |
T value; | |
Func<T> valueFactory; | |
public Lazy() : this(() => default(T)) {} | |
public Lazy(Func<T> valueFactory) | |
{ | |
this.valueFactory = valueFactory ?? (() => default(T)); | |
} | |
public bool IsValueCreated { get { return valueFactory == null; } } | |
public T Value | |
{ | |
get | |
{ | |
if (!IsValueCreated) | |
{ | |
value = valueFactory(); | |
valueFactory = null; | |
} | |
return value; | |
} | |
} | |
public override string ToString() | |
{ | |
if (!IsValueCreated) return "値は作成されていません"; | |
return value.ToString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
string 型だと、パラメータ受け取らないコンストラクタがないため、
where T : new() を削除し、
上記修正とともに () => new T(); も () => default(T) にしました。