Xan*_*iff 2 .net c# generics compiler-errors
我在使用下面的代码时遇到了麻烦,希望有人可以告诉我它有什么问题.
我给出的错误是:
无法隐式转换
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
}
}
Run Code Online (Sandbox Code Playgroud)
思考?我感谢您的帮助!
这些方法doSomething和doSomethingElse具有返回类型T,而你实际上返回thisThing<T>的这些方法的身体.这些都不一样.
举一个简单的例子,这相当于返回List<T>你期望的地方T- 它们是完全不同的类.