运算符'=>' 的作用是什么? 在 C# 中是什么意思?

发布于 2024-07-13 22:47:24 字数 207 浏览 2 评论 0原文

'=>' 是什么意思 在此声明中表示?

del = new SomeDelegate(() => SomeAction());

上面的声明和这个一样吗?

del = new SomeDelegate(this.SomeAction);

谢谢。

What does the '=>' in this statement signify?

del = new SomeDelegate(() => SomeAction());

Is the above declaration the same as this one?

del = new SomeDelegate(this.SomeAction);

Thanks.

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

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

发布评论

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

评论(4

知足的幸福 2024-07-20 22:47:24

基本上它指定一个匿名函数,不带任何参数来调用 SomeAction。 所以是的,它们在功能上是等效的。 虽不相等。 使用 lambda 更相当于:

del = new SomeDelegate(this.CallSomeAction);

其中 CallSomeAction 定义为:

public void CallSomeAction()
{
    this.SomeAction();
}

希望有帮助!

Basically it's specifying an anonymous function, that takes no parameters that calls SomeAction. So yes, they are functionally equivalent. Though not equal. Using the lambda is more equivalent to:

del = new SomeDelegate(this.CallSomeAction);

where CallSomeAction is defined as:

public void CallSomeAction()
{
    this.SomeAction();
}

Hope that helps!

原来是傀儡 2024-07-20 22:47:24

“=>” 可以读作“转到”(来源:Eric Lippert),并简单地将参数与 lambda 表达式中的操作分开。 在这种情况下,lambda 就显得有些过分了。 更好的例子是:(

var subList = list.FindAll(item => item.Type == "Foo");

查找项目类型为 Foo 的所有项目)

等。在 C# 2.0 中,也可以这样写:

var subList = list.FindAll(delegate(SomeType item) {
   return item.Type == "Foo";});

And 是表达函数“内联”的快速方式,同时还提供“闭包”支持 - 即也可能是:

string itemType = ...
var subList = list.FindAll(item => item.Type == itemType);

否则,需要类型定义来传入项目类型:

class Searcher {
    public string itemType;
    public bool Find(SomeType item) {return item.Type == itemType;}
}
...
Searcher searcher = new Searcher();
searcher.itemType = ...
var subList = list.FindAll(searcher.Find);

事实上,这几乎正是编译器为我们所做的事情(对于“委托”和 lambda 用法)。 最大的区别是 lambda可以表达表达式,例如 LINQ。

The "=>" can be read "goes to" (source: Eric Lippert), and simply separates the argument(s) from the operation in a lambda expression. In this case, a lambda is overkill. Better examples would be:

var subList = list.FindAll(item => item.Type == "Foo");

(find all items where the item's type is Foo)

etc. In C# 2.0, this can also be written:

var subList = list.FindAll(delegate(SomeType item) {
   return item.Type == "Foo";});

And is a quick way of expression a function "inline", while also offering "closure" support - i.e. it could also be:

string itemType = ...
var subList = list.FindAll(item => item.Type == itemType);

To do this otherwise would require a type-definiton to pass in the item-type:

class Searcher {
    public string itemType;
    public bool Find(SomeType item) {return item.Type == itemType;}
}
...
Searcher searcher = new Searcher();
searcher.itemType = ...
var subList = list.FindAll(searcher.Find);

In fact, this is pretty-much exactly what the compiler does for us (both for "delegate" and lambda usage). The biggest difference is that a lambda can also express an Expression, for example for LINQ.

最美的太阳 2024-07-20 22:47:24

它们相同的事情,但“() => ...”语法就是所谓的 lambda 表达式,因此与匿名函数相同。 您可能可以完全省略委托部分,只让编译器为您推断委托类型。

del = this.SomeAction;

取决于“del”被声明为什么类型。

编辑

使用 lambda 或匿名方法或仅使用常规方法(对于初学者来说)使您能够将没有委托签名的方法映射到委托。

例如,假设您有一个签名为 bool myDelegate(int, int) 的委托,但您希望有一个签名为 bool myMethod(string, string) 的方法来处理该委托。 然后,您可以使用 lambda 表达式,通过像这样的简短语法内联执行此操作。

delegate bool myDelegate(int x, int y);

// With lambdas... 
myDelegate del = (int a, int b) => myMethod(a.ToString(), b.ToString());

// With anonymous methods...
myDelegate del2 = delegate(int a, int b) { return myMethod(a.ToString(), b.ToString()); };

// The good ol' way without lambdas or anonymous methods...
myDelegate del3 = SomeDelegateHandler;

... then write a method somewhere else ...

// Handler method
private bool SomeDelegateHandler(int a, int b)
{
  return myMethod(a.ToString(), b.ToString());
}

正如您所看到的,lambda 和匿名方法基本上只是一种更短/内联的方式来创建处理委托的方法。 在您的情况下,您可能不需要创建额外的方法。 这仅取决于委托签名是否与您的方法签名相同,对我来说,似乎是这样。

They do the same thing but the "() => ..." syntax is what is called a lambda expression and as such is the same as an anonymous function. You could probably leave out the delegate part altogether and just let the compiler infer the delegate type for you.

del = this.SomeAction;

Depending on what type "del" is seclared as.

Edit

Using lambdas or anonymous methods or just a regular method for starters enables you to map methods that didn't have the delegate's signature to the delegate.

For example, say you have a delegate with the signature bool myDelegate(int, int) but you wanted to have a method with the signature bool myMethod(string, string) handle the delegate. You could then use the lambda expression to let you do this inline with a short syntax like so.

delegate bool myDelegate(int x, int y);

// With lambdas... 
myDelegate del = (int a, int b) => myMethod(a.ToString(), b.ToString());

// With anonymous methods...
myDelegate del2 = delegate(int a, int b) { return myMethod(a.ToString(), b.ToString()); };

// The good ol' way without lambdas or anonymous methods...
myDelegate del3 = SomeDelegateHandler;

... then write a method somewhere else ...

// Handler method
private bool SomeDelegateHandler(int a, int b)
{
  return myMethod(a.ToString(), b.ToString());
}

So as you can see lambdas and anonymous methods are basically just a shorter/inline way of making a method to handle the delegate. In you case you might not need to make an extra method. It just depends on if the delegate signature is the same as your method signature, and to me, it seems like it is.

昔日梦未散 2024-07-20 22:47:24

=>Lambda 运算符 , lambda 表达式就像 C# 2.0 匿名方法的演变

您可以以非常相似的方式使用匿名方法和 lambda 表达式来创建委托实例:

Func<string,int> wordCount;
wordCount = delegate (string text) { return text.Split().Length; };
Console.WriteLine (wordCount("Hello World"));

使用 lambda 表达式:

Func<string,int> wordCount;
wordCount = (string text) => { text.Split().Length; };
Console.WriteLine (wordCount("Hello World"));

=> is the Lambda Operator, lambda expressions are like an evolution of the C# 2.0 anonymous methods.

You can use anonymous methods and lambda expressions in a very similar way to create delegate instances:

Func<string,int> wordCount;
wordCount = delegate (string text) { return text.Split().Length; };
Console.WriteLine (wordCount("Hello World"));

Using lambda expressions:

Func<string,int> wordCount;
wordCount = (string text) => { text.Split().Length; };
Console.WriteLine (wordCount("Hello World"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文