Does a static method share its local variables & what happens during concurrent usage from different threads?

后端 未结 4 1750
故里飘歌
故里飘歌 2021-01-17 15:21

C# Question - I\'m trying to determine whether it is OK to use a static method where, within the method it does have some local variables it uses. Are the local variables \

4条回答
  •  臣服心动
    2021-01-17 15:48

    Local variables within the method live on the stack and each thread has its own stack. Therefore it's safe for multiple threads to use the method.

    However if the method itself uses static variables then you should use appropriate MT protection. Also external methods you may be calling need to be safe...

提交回复
热议问题