SoFunction
Updated on 2025-03-08

Summary of the role of fixed keywords in C#

in principle:

1. Garbage recycling mechanism, maintain reference information but not pointer information

2. Instantiated objects of reference type are processed by garbage collection mechanism during their lifetime, and memory may be moved.

3. When an instantiation object of a class contains value types, define a pointer to these value types to compile errors. Because these value type instances embedded in the reference type will move with the memory of the reference instantiation object, the pointer value will change without knowing it. The fixed keyword does the job of making such a class instantiation object (not the class type itself, but an instantiation object) fixed and not move, so that the garbage collection mechanism will specially handle the instantiation object of this class.