简体   繁体   English

在C#中使用委托的名称是什么?

[英]What is the name for this usage of delegate in C#?

This is a terminology question. 这是一个术语问题。 In C#, I can do this: 在C#中,我可以这样做:

delegate Stream StreamOpenerDelegate(String name);

void WorkMethod(StreamOpenerDelegate d)
{
    // ...
}

void Exec1()
{
    WorkMethod((x) =>
        {
            return File.OpenRead(x);
        });
}

void Exec2()
{
    StreamOpenerDelegate opener = (x) =>
        {
            return File.OpenRead(x);
        };

    WorkMethod(opener);
}

Q1 Q1
The Exec1() method demonstrates the use of an anonymous delegate , correct? Exec1()方法演示了匿名委托的使用,对吗?

Q2 Q2
Inside Exec2(), would opener be considered an anonymous delegate ? 在Exec2()中, opener会被视为匿名委托吗? It does have a name. 确实有一个名字。 If it's not an anonymous delegate, what should I call it? 如果它不是匿名代表,我该怎么称呼它? Is there a name for this syntax? 这个语法有名称吗? "named anonymous delegate?" “命名为匿名代表?” a local variable holding an anonymous delegate? 一个持有匿名代表的局部变量?

Q1: There's no such term as "anonymous delegate" (in the C# language specification) - but this uses a lambda expression which is one kind of anonymous function . Q1:没有“匿名委托”这样的术语(在C#语言规范中) - 但是它使用lambda表达式 ,它是一种匿名函数 See section 7.14 of the C# language specification for details. 有关详细信息,请参阅C#语言规范的第7.14节。

Q2: opener is a variable. Q2: opener是变量。 The variable is assigned a value created using a lambda expression. 为变量分配使用lambda表达式创建的值。 After it's been created, the delegate is just an instance of StreamOpenerDelegate . 创建后,委托只是StreamOpenerDelegate一个实例。 In other words, the concepts of lambda expression, anonymous function and anonymous method are source code concepts rather than execution time concepts. 换句话说,lambda表达式,匿名函数和匿名方法的概念是源代码概念而不是执行时间概念。 The CLR doesn't care how you created the delegate. CLR不关心您如何创建委托。

By the way, both of your lambda expressions can be expressed more concisely - fewer parentheses etc: 顺便说一下,你的两个lambda表达式可以更简洁地表达 - 更少的括号等:

void Exec1()
{
    WorkMethod(x => File.OpenRead(x));
}

void Exec2()
{
    StreamOpenerDelegate opener = x => File.OpenRead(x);
    WorkMethod(opener);
}

alternatively you could just use a method group conversion : 或者你可以使用方法组转换

StreamOpenerDelegate opener = File.OpenRead;

No and no. 不,不。

A1 : This feature is new to C# 3.0 and is called a lambda expression . A1 :此功能是C#3.0的新功能,称为lambda表达式 C# 2.0 has a similar feature called anonymous methods ; C#2.0有一个类似的功能,称为匿名方法 ; for example: 例如:

button.Click += delegate {
    //code
};

A2 : opener is a regular variable that happens to hold a lambda expression. A2opener是一个碰巧持有lambda表达式的常规变量。

By the way, a lambda expression that takes exactly one parameter doesn't need parentheses. 顺便说一下,只需要一个参数的lambda表达式就不需要括号。 Also, a lambda expression that consists only of a return statement doesn't need braces. 此外,仅包含return语句的lambda表达式不需要大括号。

For example: 例如:

StreamOpenerDelegate opener = x => File.OpenRead(x);

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

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