【发布时间】:2012-01-17 11:16:23
【问题描述】:
我在使用下面的代码时遇到了问题,希望有人能告诉我它有什么问题。
我得到的错误是:
无法将类型
ThisThing<T>隐式转换为T
我的代码:
class ThisThing<T>
{
public string A { get; set; }
public string B { get; set; }
}
class OtherThing
{
public T DoSomething<T>(string str)
{
T foo = DoSomethingElse<T>(str);
return foo;
}
private T DoSomethingElse<T>(string str)
{
ThisThing<T> thing = new ThisThing<T>();
thing.A = "yes";
thing.B = "no";
return thing; // This is the line I'm given the error about
}
}
想法?感谢您的帮助!
【问题讨论】:
-
请以大写字母开头类型名称,这样更容易阅读。并且,请不要使用下划线开头的局部变量。或者,如果您这样做,请以下划线开头。这将帮助我们(更重要的是您)阅读代码并防止错误。
标签: c# .net generics compiler-errors