.NET structures are value types, which means that if function A creates a structure and calls function B that tries to change the structure, B will get a new copy of the structure, so the changes will not be applied to structure A.
Structures can be much larger than other types of CLR values. Suppose that one function creates a large structure, calls another, and transfers the structure of the function. This continues for 10 levels, but all functions just have to read data from the structure and not change any of the fields. If a new copy of the structure is created in each function call, the above scenario will result in the distribution of many unnecessary structures.
C-language users can avoid this by passing a pointer instead of the structure itself, and if internal functions should not change this data in the structure, the const keyword can be used.
However, C # has links instead of pointers, and there is no such thing as a const ref.
My question is: Is .NET optimized in such a way that it knows how to copy a structure only when a function tries to change the fields inside, or that a new copy is always created after the structure is transferred to another function?
reish source
share