1. <i id='2eqW4'><tr id='2eqW4'><dt id='2eqW4'><q id='2eqW4'><span id='2eqW4'><b id='2eqW4'><form id='2eqW4'><ins id='2eqW4'></ins><ul id='2eqW4'></ul><sub id='2eqW4'></sub></form><legend id='2eqW4'></legend><bdo id='2eqW4'><pre id='2eqW4'><center id='2eqW4'></center></pre></bdo></b><th id='2eqW4'></th></span></q></dt></tr></i><div id='2eqW4'><tfoot id='2eqW4'></tfoot><dl id='2eqW4'><fieldset id='2eqW4'></fieldset></dl></div>

        <small id='2eqW4'></small><noframes id='2eqW4'>

        <legend id='2eqW4'><style id='2eqW4'><dir id='2eqW4'><q id='2eqW4'></q></dir></style></legend>
          <bdo id='2eqW4'></bdo><ul id='2eqW4'></ul>

        <tfoot id='2eqW4'></tfoot>
      1. 无法确定条件表达式的类型 (Func)

        Type of conditional expression cannot be determined (Func)(无法确定条件表达式的类型 (Func))
        <legend id='Frbyo'><style id='Frbyo'><dir id='Frbyo'><q id='Frbyo'></q></dir></style></legend>
          <tbody id='Frbyo'></tbody>

              1. <small id='Frbyo'></small><noframes id='Frbyo'>

                <i id='Frbyo'><tr id='Frbyo'><dt id='Frbyo'><q id='Frbyo'><span id='Frbyo'><b id='Frbyo'><form id='Frbyo'><ins id='Frbyo'></ins><ul id='Frbyo'></ul><sub id='Frbyo'></sub></form><legend id='Frbyo'></legend><bdo id='Frbyo'><pre id='Frbyo'><center id='Frbyo'></center></pre></bdo></b><th id='Frbyo'></th></span></q></dt></tr></i><div id='Frbyo'><tfoot id='Frbyo'></tfoot><dl id='Frbyo'><fieldset id='Frbyo'></fieldset></dl></div>
                  <bdo id='Frbyo'></bdo><ul id='Frbyo'></ul>

                  <tfoot id='Frbyo'></tfoot>
                • 本文介绍了无法确定条件表达式的类型 (Func)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  将方法分配给 Func-type 时,我收到编译错误 无法确定条件表达式的类型,因为方法组"和方法组"之间没有隐式转换'.

                  When assigning a method to a Func-type, I get the compilation error Type of conditional expression cannot be determined because there is no implicit conversion between 'method group' and 'method group'.

                  这只发生在 上?: 运算符.代码:

                  This only happens with the ? : operator. The code:

                  public class Test
                  {
                      public static string One(int value)
                      {
                          value += 1;
                          return value.ToString();
                      }
                      public static string Two(int value)
                      {
                          value += 2;
                          return value.ToString();
                      }
                      public void Testing(bool which)
                      {
                          // This works
                          Func<int, string> actionWorks;
                          if (which) actionWorks = One; else actionWorks = Two;
                  
                          // Compilation error on the part "One : Two"
                          Func<int, string> action = which ? One : Two;
                      }
                  }
                  

                  我发现 一些关于协变和逆变的信息,但我不明白这如何适用于上述情况.为什么这不起作用?

                  I found some information about co- and contravariance, but I don't see how that applies to the situation above. Why doesn't this work?

                  推荐答案

                  您需要显式提供至少一个方法组的签名.但是,在这样做之后,编译器将允许您将 action 声明为隐式类型的本地:

                  You need to explicitly provide the signature of at least one method group. However, after doing it the compiler will allow you to declare action as an implicitly-typed local:

                  var action = which ? (Func<int, string>)One : Two;
                  

                  发生这种情况的原因是 operator ?: 的返回类型不是根据您尝试分配给它的内容推导出来的,而是根据两个表达式的类型推导出来的.如果类型相同或者它们之间存在隐式转换,则编译器推导出返回类型成功;否则,它会抱怨没有转换.

                  The reason this happens is that the return type of operator ?: is not deduced based on what you are trying to assign it to, but based on the types of the two expressions. If the types are the same or there is an implicit conversion between them, the compiler deduces the return type successfully; otherwise, it complains that there is no conversion.

                  这篇关于无法确定条件表达式的类型 (Func)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                  Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                  How to store delegates in a List(如何将代表存储在列表中)
                  How delegates work (in the background)?(代表如何工作(在后台)?)
                  C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                  Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)
                        <tbody id='l2twp'></tbody>
                        <i id='l2twp'><tr id='l2twp'><dt id='l2twp'><q id='l2twp'><span id='l2twp'><b id='l2twp'><form id='l2twp'><ins id='l2twp'></ins><ul id='l2twp'></ul><sub id='l2twp'></sub></form><legend id='l2twp'></legend><bdo id='l2twp'><pre id='l2twp'><center id='l2twp'></center></pre></bdo></b><th id='l2twp'></th></span></q></dt></tr></i><div id='l2twp'><tfoot id='l2twp'></tfoot><dl id='l2twp'><fieldset id='l2twp'></fieldset></dl></div>

                        <legend id='l2twp'><style id='l2twp'><dir id='l2twp'><q id='l2twp'></q></dir></style></legend>
                          <bdo id='l2twp'></bdo><ul id='l2twp'></ul>

                            <small id='l2twp'></small><noframes id='l2twp'>

                            <tfoot id='l2twp'></tfoot>