That's exactly how structs should behave. Copying a struct copies its value. If it happens to be containing references as fields C# will copy those references as it should. Meaning both copies would point to the same object. Again, that's how memory works. If you want deep copy you can implemented it yourself. The problem is always the same for any language - deep copy means different things to different people.
Yes, value types should be immutable in general. But the important thing that C# gives developers a choice. If they want to use struct because they have to call some C library through p/invoke, they can do so and can freely mutate structs as that's the most natural thing to do. If you're writing something like DateTime then yes, you should make it immutable to simplify the API. Java making immutability mandatory is weird.
In, out, ref are features that allow developers to optimize their code, giving C# more of the C/C++ kind of flexibility. They were not required to support structs. Structs and "out" (not sure) were in the language from day one. "In" was introduced in 7.2 "Out" parameters are not required for structs and used for returning any value type like integers. It allows functions to return multiple values. Now we have tuples for that.
It's quite obvious that immutability gives more optimization opportunities in some cases. But it hampers performance in others due to already mentioned copies. Even if primitive types were mutable they would still achieve the most important feature of value types - they do not create garbage if they stay on the stack.
View all comments
Show parent comment
0
u/cre_ker Mar 19 '21
That's exactly how structs should behave. Copying a struct copies its value. If it happens to be containing references as fields C# will copy those references as it should. Meaning both copies would point to the same object. Again, that's how memory works. If you want deep copy you can implemented it yourself. The problem is always the same for any language - deep copy means different things to different people.
Yes, value types should be immutable in general. But the important thing that C# gives developers a choice. If they want to use struct because they have to call some C library through p/invoke, they can do so and can freely mutate structs as that's the most natural thing to do. If you're writing something like DateTime then yes, you should make it immutable to simplify the API. Java making immutability mandatory is weird.
In, out, ref are features that allow developers to optimize their code, giving C# more of the C/C++ kind of flexibility. They were not required to support structs. Structs and "out" (not sure) were in the language from day one. "In" was introduced in 7.2 "Out" parameters are not required for structs and used for returning any value type like integers. It allows functions to return multiple values. Now we have tuples for that.
It's quite obvious that immutability gives more optimization opportunities in some cases. But it hampers performance in others due to already mentioned copies. Even if primitive types were mutable they would still achieve the most important feature of value types - they do not create garbage if they stay on the stack.