一个泛型的问题?C#泛型代码怎么修改? 你要干嘛?方法返回类型是void,你还return new T()? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 除了上面说的返回的问题,想 new T() 就必须约束T有公开的无参构造:void GetEntity<T>() where T : new() 没看懂想要干嘛如果是要返回T,那就要说明T是不是class,那就应该是类似下面的代码T GetEntity<T>() where T:new(){if (this.Value == null){this.Value = new T();}return this.Value;} 如果是要返回默认值,那就应该是下面类似的代码T Get<T>(){ return default(T);} T GetEntity<T>() where T : new() { return (T)(Value ?? (Value=new T())); } 代码问题太多,直接写一个给你吧:T GetEntity<T>() : where T : class, new() { 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; } net用什么加密源码软件??? 关于c#中数据库连接的问题 C#皮肤控件问题 这样的语句是用来干嘛的啊??? 终于要用2005了 好像晚了一些 大家说是不是得去买笨书看看呢 类型转换的问题,如何转换类型为强类型 戏弄c# 查找excel一列不为空的数据量 关于值的获取 小问题:system.TypeLoadException是怎么回事! C#叫号系统如何编写和调用webservic? 泛型如何读取变量?这里这么写肯定是不对的。请问怎么写?
如果是要返回T,那就要说明T是不是class,那就应该是类似下面的代码T GetEntity<T>()
where T:new()
{
if (this.Value == null)
{
this.Value = new T();
}
return this.Value;
} 如果是要返回默认值,那就应该是下面类似的代码
T Get<T>()
{
return default(T);
}
{
return (T)(Value ?? (Value=new T()));
}
T GetEntity<T>() : where T : class, new()
{
if (this.Value == null)
this.Value = new T();
return new T();
}
{
if (this.Value == null)
this.Value = new T();
return this.Value;
}