Closures in C# - initating a thread in a function call with a value type

前端 未结 3 607
天涯浪人
天涯浪人 2021-01-28 17:43

I have this code, which works as I wanted but I don\'t understand exactly why. Thinking about a stack in C, C++, I\'d guess that the p variable will be on the stack on each call

3条回答
  •  孤街浪徒
    2021-01-28 18:14

    How does the closure of the thread captures it and more over, captures the correct value every time?

    That is compiler magic. Simply because the p parameter is being used by the lambda the compiler treats it differently. p is not placed on the stack but on the heap. That is why it still exists after callme() has terminated.

提交回复
热议问题