The other day, in one of my utilities, ReSharper hinted to me a piece of code that says that the lambda that defines the ThreadStart delegate can be turned into a local function:
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(); } }
And, therefore, it is converted to:
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(); } }
What are the advantages of the latter compared to the former, is it only about performance?
I already checked the resources below, but in my example the benefits are not so obvious:
source share