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

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

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

        隐式和显式委托创建之间的区别(有和没有泛型)

        时间:2023-11-11

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

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

                • <bdo id='P4IjK'></bdo><ul id='P4IjK'></ul>
                • <tfoot id='P4IjK'></tfoot>
                  本文介绍了隐式和显式委托创建之间的区别(有和没有泛型)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  请看下面 Go() 方法中的四行代码:

                  delegate void Action<T>(T arg);
                  delegate void Action();
                  
                  void DoSomething<T>(Action<T> action)
                  {
                      //...
                  }
                  
                  void DoSomething(Action action)
                  {
                      //...
                  }
                  
                  void MyAction<T>(T arg)
                  {
                      //...
                  }
                  
                  void MyAction()
                  {
                      //...
                  }
                  
                  void Go<T>()
                  {
                      DoSomething<T>(MyAction<T>); // throws compiler error - why?
                      DoSomething(new Action<T>(MyAction<T>)); // no problems here
                      DoSomething(MyAction); // what's the difference between this...
                      DoSomething(new Action(MyAction)); // ... and this?
                  }
                  

                  请注意,第一次调用产生的编译器错误是:方法Action(T)"的类型参数不能从用法中推断出来.尝试明确指定类型参数.

                  Note that the compiler error generated by the first call is: The type arguments for method 'Action(T)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

                  推荐答案

                  MyActionnew Action(MyAction) 没有区别(当它们都有效时)除了前者之外,在 C# 1 中不起作用.这是一个隐式方法组转换.有时这不适用,尤其是当编译器无法确定您想要什么样的委托时,例如

                  There's no difference between MyAction and new Action(MyAction) (when they're both valid) other than the former won't work in C# 1. This is an implicit method group conversion. There are times that this isn't applicable, most notable when the compiler can't work out what kind of delegate you want, e.g.

                  Delegate foo = new Action(MyAction); // Fine
                  Delegate bar = MyAction; // Nope, can't tell target type
                  

                  这在您的问题中起作用,因为所涉及的两种方法都已重载.这基本上会导致头痛.

                  This comes into play in your question because both of the methods involved are overloaded. This leads to headaches, basically.

                  至于泛型方面 - 这很有趣.方法组并没有从 C# 3 类型推断中得到太多的喜爱——我不确定这是否会在 C# 4 中得到改进.如果您调用泛型方法并指定类型参数,则类型推断工作得相当好 - 但如果您尝试反过来做,它会失败:

                  As for the generics side - it's interesting. Method groups don't get much love from C# 3 type inference - I'm not sure whether that's going to be improved in C# 4 or not. If you call a generic method and specify the type argument, type inference works fairly well - but if you try to do it the other way round, it fails:

                  using System;
                  
                  class Test
                  {
                      static void Main()
                      {
                          // Valid - it infers Foo<int>
                          DoSomething<int>(Foo);
                          // Valid - both are specified
                          DoSomething<int>(Foo<int>);
                          // Invalid - type inference fails
                          DoSomething(Foo<int>);
                          // Invalid - mismatched types, basically
                          DoSomething<int>(Foo<string>);
                      }
                  
                      static void Foo<T>(T input)
                      {
                      }
                  
                      static void DoSomething<T>(Action<T> action)
                      {
                          Console.WriteLine(typeof(T));
                      }
                  }
                  

                  C# 3 中的类型推断非常复杂,在大多数情况下都能很好地工作(特别是它对 LINQ 非常有用),但在其他一些情况下却失败了.在理想的世界中,它会变得更容易理解并且在未来的版本中会更强大......我们拭目以待!

                  Type inference in C# 3 is very complicated, and works well in most cases (in particular it's great for LINQ) but fails in a few others. In an ideal world, it would become easier to understand and more powerful in future versions... we'll see!

                  这篇关于隐式和显式委托创建之间的区别(有和没有泛型)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:C# 编译错误:“在创建窗口句柄之前,不能对控件调用 Invoke 或 BeginInvoke." 下一篇:使用委托会减慢我的 .NET 程序的速度吗?

                  相关文章

                  1. <small id='0AIGw'></small><noframes id='0AIGw'>

                    • <bdo id='0AIGw'></bdo><ul id='0AIGw'></ul>

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

                      <tfoot id='0AIGw'></tfoot>