void GetEntity() {
if (this.Value == null) this.Value = new T(); // 編譯錯誤:T 必須有 new()
return new T();
}
T GetEntity() where T : class, new() {
if (this.Value == null) this.Value = new T();
return this.Value; // 返回已創建實例
}
class:限定引用類型 (排除 struct)。new():要求公共無參構造函數。where T : struct + new() (C# 7.0+ 支持 default(T) 無需 new)。var entity = GetEntity(); // MyEntity 必須有 public MyEntity()