<tfoot id='wLViL'></tfoot>
    • <bdo id='wLViL'></bdo><ul id='wLViL'></ul>
    <legend id='wLViL'><style id='wLViL'><dir id='wLViL'><q id='wLViL'></q></dir></style></legend>

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

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

    1. 即使签名匹配,也无法将一种类型的委托分配给另一种类型

      时间:2023-06-09
      • <bdo id='gNXzH'></bdo><ul id='gNXzH'></ul>

            <legend id='gNXzH'><style id='gNXzH'><dir id='gNXzH'><q id='gNXzH'></q></dir></style></legend>

            <tfoot id='gNXzH'></tfoot>
              <tbody id='gNXzH'></tbody>

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

            1. <i id='gNXzH'><tr id='gNXzH'><dt id='gNXzH'><q id='gNXzH'><span id='gNXzH'><b id='gNXzH'><form id='gNXzH'><ins id='gNXzH'></ins><ul id='gNXzH'></ul><sub id='gNXzH'></sub></form><legend id='gNXzH'></legend><bdo id='gNXzH'><pre id='gNXzH'><center id='gNXzH'></center></pre></bdo></b><th id='gNXzH'></th></span></q></dt></tr></i><div id='gNXzH'><tfoot id='gNXzH'></tfoot><dl id='gNXzH'><fieldset id='gNXzH'></fieldset></dl></div>
              • 本文介绍了即使签名匹配,也无法将一种类型的委托分配给另一种类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我病态的好奇心让我想知道为什么以下失败:

                My morbid curiosity has me wondering why the following fails:

                // declared somewhere
                public delegate int BinaryOperation(int a, int b);
                
                // ... in a method body
                Func<int, int, int> addThem = (x, y) => x + y;
                
                BinaryOperation b1 = addThem; // doesn't compile, and casting doesn't compile
                BinaryOperation b2 = (x, y) => x + y; // compiles!
                

                推荐答案

                C# 对结构"类型的支持非常有限.特别是,您不能将一种委托类型转换为另一种简单地,因为它们的声明是相似的.

                C# has very limited support for "structural" typing. In particular, you can't cast from one delegate-type to another simply because their declarations are similar.

                来自语言规范:

                C# 中的委托类型是名称等效的,不是结构上的相等的.具体来说,两个不同的委托类型具有相同的参数列表和返回类型被认为是不同的代表类型.

                Delegate types in C# are name equivalent, not structurally equivalent. Specifically, two different delegate types that have the same parameter lists and return type are considered different delegate types.

                尝试以下之一:

                // C# 2, 3, 4 (C# 1 doesn't come into it because of generics)
                BinaryOperation b1 = new BinaryOperation(addThem);
                
                // C# 3, 4
                BinaryOperation b1 = (x, y) => addThem(x, y);
                var b1 = new BinaryOperation(addThem);
                

                这篇关于即使签名匹配,也无法将一种类型的委托分配给另一种类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何查找和调用特定类型的 .Net TypeConverter? 下一篇:检查字符串是否可以转换为 C# 中的给定类型

                相关文章

                1. <tfoot id='UVuHa'></tfoot>

                    <bdo id='UVuHa'></bdo><ul id='UVuHa'></ul>

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

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