简体   繁体   English

“ =>”是什么意思?

[英]What does “=>” mean?

Forgive me if this screams newbie but what does => mean in C#? 如果这叫新手,请原谅我,但是在C#中=>是什么意思? I was at a presentation last week and this operator (I think) was used in the context of ORM. 上周我在一次演讲中,这个运算符(我认为)是在ORM中使用的。 I wasn't really paying attention to the specifics of syntax until I went back to my notes. 在回到笔记之前,我并没有真正关注语法的细节。

In C# the lambda operator is written "=>" (usually pronounced " goes to " when read aloud). 在C#中, lambda运算符写为“ =>”(大声读取时通常发音为“ go to ”)。 It means that the arguments on the left are passed into the code block (lambda function / anonymous delegate) on the right. 这意味着左侧的参数将传递到右侧的代码块(lambda函数/匿名委托)中。

So if you have a Func or Action (or any of their cousins with more type parameters) then you can assign a lambda expression to them rather than needing to instantiate a delegate or have a separate method for the deferred processing: 因此,如果您具有Func或Action(或它们的任何表亲带有更多类型参数),则可以为它们分配一个lambda表达式,而无需实例化委托或使用单独的方法进行延迟处理:

//creates a Func that can be called later
Func<int,bool> f = i => i <= 10;
//calls the function with 12 substituted as the parameter
bool ret = f(12);

Since nobody mentioned it yet, in VB.NET you'd use the function keyword instead of =>, like so: 由于尚未有人提到它,因此在VB.NET中,您将使用function关键字而不是=>,如下所示:

dim func = function() true
'or
dim func1 = function(x, y) x + y

dim result = func() ' result is True
dim result1 = func1(5, 2) ' result is 7

It's shorthand for declaring a lambda . 这是声明lambda的简写。

i => i++

is (sort of) the same as writing: 与写作相同(有点):

delegate(int i)
{
    i++;
}

In the context of: 在以下情况下:

void DoSomething(Action<int> doSomething)
{
    doSomething(1);
}

DoSomething(delegate(int i) { i++; });
//declares an anonymous method
//and passes it to DoSomething

which is (sort of) the same as writing: 与写作相同(有点):

void increment(int i)
{
    i++;
}

Just without giving it a name, it allows you to declare a function in-line, known as an "anonymous" function. 只是不给它一个名称,它允许您直接声明一个函数,称为“匿名”函数。

When said aloud the operator is the lambda ( goes to ) operator which helps to define the anonymous delegate that you're defining in the lambda. 大声说出来时,运算符是lambda( 转到 )运算符,它有助于定义您在lambda中定义的匿名委托。

A common place to see this is with an event handler. 一个常见的地方是事件处理程序。 You will often have aa page load type event that is handled by a lambda with the following code: 您经常会遇到一个页面加载类型事件,该事件由lambda使用以下代码处理:

this.Loaded += (o, e) => { 

// code

}

You've defined a method handling the Loaded event anonymously (it doesn't have a name) by using a lambda expression. 您已经定义了一个使用lambda表达式来匿名处理Loaded事件的方法(它没有名称)。 It would read as "o, e goes to ... method definition with foo." 它将显示为“ o,e使用foo前往...方法定义”。

This is the "lambda operator", and you read it as "goes to". 这是“ lambda运算符”,您将其读为“ goes to”。 Say you had the statement: 假设您有以下陈述:

doSomething(x => x + " hi");

You can replace the "=>" in your mind with this: 您可以将“ =>”替换为:

doSomething(delegate (string x) { return x + " hi" });

As you can see, it offers a heck of a shorthand. 如您所见,它提供了一些速记。 The compiler figures out the type of the variable that you're passing, and allows you to get rid of the function signature and bracketing for the code that you're passing the signature variables into. 编译器会确定要传递的变量的类型,并允许您将函数签名和传递括号的代码放入括号中。

It's a lambda operator, part of a lambda expression . 它是lambda运算符,是lambda表达式的一部分。

All lambda expressions use the lambda operator =>, which is read as "goes to". 所有lambda表达式都使用lambda运算符=>,它被读为“ goes to”。 The left side of the lambda operator specifies the input parameters (if any) and the right side holds the expression or statement block. lambda运算符的左侧指定输入参数(如果有),而右侧则保存表达式或语句块。 The lambda expression x => x * x is read "x goes to x times x." 读取lambda表达式x => x * x“ x达到x的x倍”。

It's syntax to declare an anonymous function, known in C# as a "lambda expression." 声明匿名函数的语法,在C#中称为“ lambda表达式”。

For example, (int p) => p * 2 represents a function that takes an integer and multiplies it by two. 例如, (int p) => p * 2表示一个函数,该函数采用整数并将其乘以2。

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

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