“==”是什么意思? 意思是?

发布于 2024-07-28 23:09:54 字数 121 浏览 4 评论 0原文

如果这让新手尖叫,请原谅我,但是 => 在 C# 中意味着什么? 我上周参加了一次演讲,这个运算符(我认为)是在 ORM 的上下文中使用的。 在我回到我的笔记之前,我并没有真正关注语法的细节。

Forgive me if this screams newbie but what does => mean in C#? I was at a presentation last week and this operator (I think) was used in the context of ORM. I wasn't really paying attention to the specifics of syntax until I went back to my notes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

忱杏 2024-08-04 23:09:54

在 C# 中,lambda 运算符 写作“=>” (大声朗读时通常发音为“”)。 这意味着左侧的参数被传递到右侧的代码块(lambda 函数/匿名委托)中。

因此,如果您有一个 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);

In C# the lambda operator is written "=>" (usually pronounced "goes to" when read aloud). It means that the arguments on the left are passed into the code block (lambda function / anonymous delegate) on the right.

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:

//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);
半暖夏伤 2024-08-04 23:09:54

由于还没有人提到它,因此在 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

Since nobody mentioned it yet, in VB.NET you'd use the function keyword instead of =>, like so:

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
幼儿园老大 2024-08-04 23:09:54

它是声明 lambda 的简写。

i => i++

与写作(有点)相同:

delegate(int i)
{
    i++;
}

在上下文中:

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

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

这与写作(有点)相同:

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

只需不给它一个名称,它就允许您内嵌声明一个函数,称为“匿名”功能。

It's shorthand for declaring a 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.

燃情 2024-08-04 23:09:54

当大声说出时,该运算符是 lambda(转到)运算符,它有助于定义您在 lambda 中定义的匿名委托。

常见的情况是使用事件处理程序。 您经常会遇到由 lambda 使用以下代码处理的页面加载类型事件:

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

// code

}

您已经使用 lambda 表达式定义了一个匿名处理 Loaded 事件的方法(它没有名称)。 它将读作“o,e 转到...使用 foo 的方法定义”。

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.

A common place to see this is with an event handler. You will often have a a page load type event that is handled by a lambda with the following code:

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. It would read as "o, e goes to ... method definition with foo."

夜司空 2024-08-04 23:09:54

这是“lambda 运算符”,您可以将其读作“前往”。 假设您有这样的语句:

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

您可以替换“=>” 在你的脑海中这样想:

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

正如你所看到的,它提供了一种速记法。 编译器会计算出您传递的变量的类型,并允许您摆脱函数签名和将签名变量传递到的代码的括号。

This is the "lambda operator", and you read it as "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.

怕倦 2024-08-04 23:09:54

它是一个 lambda 运算符,是 lambda 表达式 的一部分。

所有 lambda 表达式都使用 lambda
运算符=>,读作“goes
到”。 lambda 的左侧
运算符指定输入
参数(如果有)和右侧
保存表达式或语句
堵塞。 lambda 表达式 x => X *
x 被读作“x 到 x 乘 x。”

It's a lambda operator, part of a lambda expression.

All lambda expressions use the lambda
operator =>, which is read as "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. The lambda expression x => x *
x is read "x goes to x times x."

2024-08-04 23:09:54

它是声明匿名函数的语法,在 C# 中称为“lambda 表达式”。

例如,(int p) => p * 2 表示一个函数,它接受一个整数并将其乘以二。

It's syntax to declare an anonymous function, known in C# as a "lambda expression."

For example, (int p) => p * 2 represents a function that takes an integer and multiplies it by two.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文