' to ' T '"-6ren"> ' to ' T '"-我在使用下面的代码时遇到了问题,希望有人能告诉我它出了什么问题。 我得到的错误是: Cannot implicitly convert type ThisThing to T 我的代码: class -6ren">
gpt4 book ai didi

c# - "Cannot implicitly convert type ' 这个方法 ' to ' T '"

转载 作者:行者123 更新时间:2023-11-30 18:50:11 26 4
gpt4 key购买 nike

我在使用下面的代码时遇到了问题,希望有人能告诉我它出了什么问题。

我得到的错误是:

Cannot implicitly convert type ThisThing<T> to 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
}
}

想法?感谢您的帮助!

最佳答案

方法 doSomethingdoSomethingElse返回类型为 T ,而您实际上返回的是 thisThing<T>在这些方法的主体中。这些不一样。

举个简单的例子,这相当于返回 List<T>你期望的地方只是T - 它们是完全不同的类别。

关于c# - "Cannot implicitly convert type ' 这个方法<T> ' to ' T '",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8893696/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com