Threading and static methods in C#

后端 未结 3 1650
广开言路
广开言路 2021-02-19 20:54

Here is a meaningless extension method as an example:

public static class MyExtensions
{
    public static int MyExtensionMethod(this MyType e)
    {
        in         


        
相关标签:
3条回答
  • 2021-02-19 21:23

    Yes, that's a correct assessment. x is a method-local variable, and won't be shared between invocations of MyExtensionMethod.

    0 讨论(0)
  • 2021-02-19 21:25

    Quite simply, yes. A static method only means that the method can be called without an object. The local variables within the method are still local.

    0 讨论(0)
  • 2021-02-19 21:28

    Yes, each thread gets its own separate local variable. This function will always return 2 even if called by multiple threads simultaneously.

    0 讨论(0)
提交回复
热议问题