Fixed Statement in C#

后端 未结 5 1889
南笙
南笙 2021-01-17 11:51

We have similar code to the following in one of our projects. Can anyone explain (in simple English) why the fixed statement is needed here?

class TestClass
         


        
5条回答
  •  孤独总比滥情好
    2021-01-17 12:31

    The fixed statement will "pin" the variable in memory so that the garbage collector doesn't move it around when collecting. If it did move the variable, the pointer would become useless and when you used it you'd be trying to access or modify something that you didn't intend to.

提交回复
热议问题