简体   繁体   English

编译器如何推断委托示例中的类型?

[英]How does the compiler infer types in the delegate example?

In the following delegate example, how does the compiler infer what type the variable alpha is? 在下面的委托示例中,编译器如何推断变量alpha的类型是什么?

delegate double Doubler(double x);

public class Test
{
    Doubler dbl = (alpha) => //How does it determine what type is alpha?
    {
        return alpha * 2
    };

    Console.WriteLine(dbl(10)); //Is it when the method is called?  int here;

    Console.WriteLine(dbl(5.5)); //double here???
}

I found this statement on a website, I guess based on the responses, is it incorrect? 我在一个网站上发现了这个声明,我想根据回答,这是不正确的?

"In our example, we specified the type of the argument. If you want, you can let the compiler figure out the type of argument. In this case, pass only the name of the argument and not its type. Here is an example:" “在我们的例子中,我们指定了参数的类型。如果你愿意,你可以让编译器找出参数的类型。在这种情况下,只传递参数的名称而不是它的类型。这是一个例子: “

You declare it in your delegate. 您在委托中声明它。

delegate double Doubler(double x);

x is your alpha. x是你的alpha。

You could easily replace your code with: 您可以使用以下命令轻松替换代码:

Doubler dbl = delegate (double x)
{
   return x*2;
};

Also you could simplify your lambda expression: 您还可以简化lambda表达式:

Doubler dbl = alpha => alpha*2;

This language feature looks simple but in fact is really quite complicated. 这种语言功能看起来很简单,但实际上非常复杂。 It would take far too much space to describe here exactly what all the consequences of this feature are. 这里需要花费太多空间来准确描述此功能的所有后果。 If you are really interested in how the compiler works out the type of the formal parameter to the lambda, you should read my five-part series of articles discussing how we do this, and what the consequences are to the compiler design. 如果您真的对编译器如何计算lambda的形参的类型感兴趣,那么您应该阅读我讨论如何执行此操作的五篇系列文章,以及对编译器设计的后果。

Here's my archive of all my articles where I discuss the implications of lambda expressions: 这是我所有文章的存档,我讨论了lambda表达式的含义:

http://blogs.msdn.com/ericlippert/archive/tags/Lambda+Expressions/default.aspx http://blogs.msdn.com/ericlippert/archive/tags/Lambda+Expressions/default.aspx

You should read the series entitled "Lambda Expressions vs. Anonymous Methods". 您应该阅读题为“Lambda表达式与匿名方法”的系列。

If you're interested in how the compiler performs other type inferences, here's my archive of articles on that: 如果您对编译器如何执行其他类型推断感兴趣,请参阅我的文章存档:

http://blogs.msdn.com/ericlippert/archive/tags/Type+Inference/default.aspx http://blogs.msdn.com/ericlippert/archive/tags/Type+Inference/default.aspx

If you're the sort of person who prefers watching video to reading text, here's a video I made in 2006 explaining some of the type inference scenarios: 如果你是那种喜欢观看视频阅读文字的人,这里是我在2006年制作的视频,解释了一些类型推断场景:

http://blogs.msdn.com/ericlippert/archive/2006/11/17/a-face-made-for-email-part-three.aspx http://blogs.msdn.com/ericlippert/archive/2006/11/17/a-face-made-for-email-part-three.aspx

You specified that the input type is double when you defined the Doubler delegate. 您在定义Doubler委托时指定了输入类型为double。 For both examples the input type is double. 对于这两个示例,输入类型是double。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM