简体   繁体   English

不确定使用 lambdas 处理这个 c# 事件是如何工作的,有人可以向我解释一下吗?

[英]Not sure how this c# event handling with lambdas work, can someone explain it to me please?

I was looking for a way to convert vb event handling to c# since i am implementing something and, after reading this post ( How do I Convert from Anonymous Event Handlers in C# to VB.Net ), i tried to copy the lambda structure, and somehow it worked, but i have NO IDEA.我一直在寻找一种将 vb 事件处理转换为 c# 的方法,因为我正在实现一些东西,并且在阅读了这篇文章后( How do I Convert from Anonymous Event Handlers in C# to VB.Net ),我尝试复制 lambda 结构,并且不知何故它起作用了,但我不知道。 How it does, can someonoe please explain it to me?它是怎么做的,有人可以给我解释一下吗? Or at least guide me to a link for a tutorial or something that helps me understand whats going on in here... I mean, i can get out with this since it, somehow, worked, but i want to understand whats going on rather than just, "get the job done".或者至少引导我到教程的链接或帮助我理解这里发生了什么的东西......我的意思是,我可以摆脱这个,因为它以某种方式起作用,但我想了解发生了什么而不是不仅仅是“完成工作”。

Class.Event += Class.Delegate((sometext)) => {eventhandlemethod(sometext);});
eventhandlemethod(string s)
{
   MessageBox.Show(s);
}

Thanks in advance!提前致谢!

I am not familiar with VB.NET but an anonymous Function or Action as it would be in the case of an Eventhandler is basically just a short form of writing.我不熟悉 VB.NET,但匿名函数或操作就像在事件处理程序的情况下一样,基本上只是一种简短的写作形式。

But what is the line但什么是线

Class.Event += Class.Delegate((sometext) => {eventhandlemethod(sometext);});

telling you?告诉你?

  1. You have an Event, and you add the handler (obviously).您有一个事件,并且添加了处理程序(显然)。
  2. You have added an anonymous method as Eventhandler and created a Delegate from it.您添加了一个匿名方法作为 Eventhandler 并从中创建了一个委托。

     Class.Delegate(<anoymous method, matching the siganture of the handler>);
  3. Your anonymous method looks like this您的匿名方法如下所示

    (sometext) => {eventhandlemethod(sometext);}

This anonymous method (short lamba) consists of two parts这个匿名方法(短lamba)由两部分组成

siganture => body

In Your case the signature gets eh "sometext".在你的情况下,签名是“sometext”。 Derived from the rest of your code, this seems to be a string.从您的其余代码派生,这似乎是一个字符串。 And since eventhandlers always have void as return value, your method, would you have written a normal method, would look like this:并且由于事件处理程序总是将 void 作为返回值,您的方法,您是否编写了一个普通方法,将如下所示:

private void (string sometext)
{
}

which is basically, what your eventhandlemethod looks like.这基本上就是您的 eventhandlemethod 的样子。 You could write this lambda like this as well.你也可以这样写这个 lambda。

(string sometext) => {eventhandlemethod(sometext);}

This shows you the expected intput type.这会向您显示预期的输入类型。 But you can ommit this usually.但是你通常可以省略这个。

So just as goodie: you could have written所以就像好东西:你可以写

  1. with a "normal" method, since you already have it使用“正常”方法,因为您已经拥有它

    Class.Event += Class.Delegate(eventhandlemethod);
  2. with a lambda使用 lambda

     Class.Event += Class.Delegate(sometext => MessageBox.Show(sometext));

if you have multiple input variables, put them in brackets如果您有多个输入变量,请将它们放在括号中

(string s, int i, bool b) => Console.WriteLine($"{s}:{i}:{b}");

and of course, you can ommit the types as well leaving you:当然,你也可以省略类型,留下你:

(s, i, b) => Console.WriteLine($"{s}:{i}:{b}");

I hope i could clarify it a bit for you.我希望我能为你澄清一下。

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

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