本文介绍了任何人都可以解释这种通用构造函数链接语法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 在尝试效仿时,我遇到了一些我以前从未见过的语法。到目前为止,我还没有在互联网上找到解释。 代码行是一个链接到另一个的构造函数。这是代码:Hi,While trying to follow an example, I came across some syntax I have not seen before. I have so far failed to find an explanation for it on the internet.The line of code is one constructor chaining to another. This is the code:public class Command{ public Command(Action<object> execute) { } public Command(Action execute) : this( (Action<object>) ( o => execute() ) ) { }} 有谁能解释一下这里发生了什么? 亲切的愿望~PatrickCan anyone please explain what is going on here?Kind wishes ~ Patrick推荐答案这是一个名为Command的类的类实例构造函数。 冒号之后的东西只是在同一个类中调用另一个同名的构造函数,传入一个Action<>被定义为lambda表达式的对象。 某处应该有另一个命令(Action<>)或者在课堂上。This is a class instance constructor for a class called "Command".The stuff after the colon is just calling another constructor of the same name in the same class, passing in an Action<> object which is defined as a lambda expression.There should be another Command(Action<>) somewhere else in the class.为了使解决方案1更准确: 为了使代码编译,应该包含它在这样的类定义中:To make the Solution 1 more accurate:To make the code compile, it should be included in the class definition like this:using System;// ...public class Command { // one constructor: public Command(Action<object> action) { // ... } // another constructor calls the constructor above: public Command(Action execute) : this((Action<object>)(o => execute())) { // no need in the method body; // it's implemented via "this(...)" } // ...} 构造函数不能是命令(动作<>); 操作<> 不会是完整类型。同时,对其他构造函数的调用清楚地表明第一个构造函数的实际泛型参数是 Action< System.Object> 。 所以,让我们看看发生了什么。我希望,第一个构造函数(你没有显示)是清楚的。它的实现方式无关紧要;但是委托实例 action 可以在此构造函数中调用,或者最终保留以便稍后调用。此委托实例具有类型 System.Object 的参数,该参数将传递给作为参数传递给第一个构造函数的委托实例。要声明所有 Action 委托,请参阅命名空间 System ,程序集System.Core。 第二个构造函数接受 System.Action 类型的参数,这是没有参数的void委托。这个构造函数的整个目的是调用上面的构造函数(在':'之后的构造部分中完成),而的技巧是忽略 System.Object 操作的参数,由 delta中的参数 o 表示表达的。构造函数实现包装方法忽略此lambda表达式中的参数。 在上面的代码示例中,使用了type cast语法,圆括号中的(完整)类型,(Action< object>)。这是因为 lambda表达式中使用的类型推断。实际上,未指定对象 o 的类型。即使忽略该参数,实现也需要知道其类型。有时,人们可以从只有一个其他构造函数的事实推断出类型;这是我必须要回答这个问题的推论。但这不是编译器类型推断的工作原理;它需要更多确定的信息。 参见:隐式类型局部变量(C#编程指南) [ ^ ]。 我不喜欢上面显示的语法,通常使用稍微不同的语法,使用构造函数:The constructor could not be of the type Command(Action<>); Action<> would not be a complete type. At the same time, the call to the other constructor clearly indicate that the actual generic parameter of the first constructor is Action<System.Object>.So, let's see what's going on. I hope, first constructor (which you did not show) is clear. It does not matter what its implementation does; but the delegate instance action can be invoked in this constructor, or just preserved to be invoked later, eventually. This delegate instance has a parameters of the type System.Object, which is passed to the delegate instance passed as a parameter to the first constructor. For the declaration of all Action delegates, see the namespace System, assembly "System.Core".The second constructor accepts the parameters of the type System.Action, which is the void delegate without a parameter. The whole purpose of this constructor call the constructor above (which is done in the part of the construct after ':'), and the trick is to ignore System.Object the parameter of the action, which is represented by the parameter o in the delta expression. The constructor implementation wraps the method ignoring the parameter in this lambda expression.In the code sample above, the "type cast" syntax is used, the (complete) type in round brackets, (Action<object>). This is done because of type inference used in lambda expressions. Indeed, the type of the object o is not specified. Even though the parameter is ignored, the implementation needs to know its type. Sometimes, one can infer the type from the fact that there is only one other constructor; this is the inference I have to perform to answer this question. But this is not how the compiler's type inference works; it needs more certain information.See also: Implicitly Typed Local Variables (C# Programming Guide)[^].I don't like the syntax shown above and usually use slightly different syntax, with a constructor:public class Command { public Command(Action<object> action) {/* ... */} public Command(Action execute) : this(new Action<object>(o => execute())) { } // ...} -SA 这篇关于任何人都可以解释这种通用构造函数链接语法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-01 12:12