gpt4 book ai didi

c# - 为什么我可以使用 lambda 表达式代替回调委托(delegate)?

转载 作者:太空狗 更新时间:2023-10-29 18:12:41 27 4
gpt4 key购买 nike

我发现了一些新的 C# 语法,但不明白它的含义。下面是语法相关的代码:

1)

BeginInvoke(new Action(() =>
{
PopulateUI(ds);
}));

2)

private void OnFormLoad() 
{
ThreadPool.QueueUserWorkItem(() => GetSqlData());
}

new Action() 是什么意思,=> 符号是什么意思?

ThreadPool.QueueUserWorkItem 的语法是 ThreadPool.QueueUserWorkItem(new WaitCallback(PrintOut), "Hello"); 但是这里显示的是 ThreadPool.QueueUserWorkItem( () => GetSqlData());,那么它是如何工作的呢?为什么 WaitCallback 不见了?请详细说明。

非常感谢。

最佳答案

看看

Action Delegate

Encapsulates a method that has a single parameter and does not return a value. You can use the Action delegate to pass a method as a parameter without explicitly declaring a custom delegate.

Lambda Expressions (C# Programming Guide)

A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types.

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."

关于c# - 为什么我可以使用 lambda 表达式代替回调委托(delegate)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4487454/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com