问题描述
- 一个泛型的问题?C#泛型代码怎么修改?
-
C#泛型代码怎么修改?
void GetEntity()
{
if (this.Value == null)
this.Value = new T();
return new T();
}
解决方案
T GetEntity<T>() where T : class, new()
{
if (this.Value == null)
this.Value = new T();
return this.Value;
}
解决方案二:
GetEntity()
解决方案三:
GetEntity《T》()
尖括号也发不了,只能用书名号了
解决方案四:
void GetEntity<T>()
{
if (this.Value == null)
this.Value = default(T);
}
解决方案五:
T GetEntity<T>() where T : class, new()
{
if (this.Value == null)
this.Value = new T();
return this.Value();
}
时间: 2024-06-16 04:10:56