gpt4 book ai didi

c# - 与 lambda 相比,C# 7 本地函数有哪些优势?

转载 作者:太空狗 更新时间:2023-10-29 17:49:06 33 4
gpt4 key购买 nike

<分区>

前几天,在我的一个实用程序中,ReSharper 向我提示了下面的一段代码,指出定义委托(delegate) ThreadStart 的 lambda 可以转换为本地函数:

public void Start(ThreadPriority threadPriority = ThreadPriority.Lowest)
{
if (!Enabled)
{
_threadCancellationRequested = false;

ThreadStart threadStart = () => NotificationTimer (ref _interval, ref _ignoreDurationThreshold, ref _threadCancellationRequested);

Thread = new Thread(threadStart) {Priority = ThreadPriority.Lowest};
Thread.Start();
}
}

因此转化为:

public void Start(ThreadPriority threadPriority = ThreadPriority.Lowest)
{
if (!Enabled)
{
_threadCancellationRequested = false;

void ThreadStart() => NotificationTimer(ref _interval, ref _ignoreDurationThreshold, ref _threadCancellationRequested);

Thread = new Thread(ThreadStart) {Priority = ThreadPriority.Lowest};
Thread.Start();
}
}

后者相对于前者有什么好处,是否只与性能有关?

我已经检查了下面的资源,但在我的示例中,好处并不那么明显:

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