All C # newbies know that class is a reference type, and struct is the value of one. Structures are recommended for use as a simple repository. They can also implement interfaces, but they cannot deduce from classes and cannot play the role of base classes because of rather "value".
Suppose we shed some light on the main differences, but there is one that haunts me. Take a look at the following code:
public class SampleClass { public void AssignThis(SampleClass data) { this = data;
This is clear, hell, of course, we are not allowed to change our own pointer to an object , despite the fact that this is done in C ++ - this is a simple practice. But:
public struct SampleStruct { public void AssignThis(SampleStruct data) { this = data;
Why does it work? It looks like struct this not a pointer. If so, how does the work above work? Is there a mechanism for automatic cloning? What happens if there is a class in the structure?
What are the main differences between class and struct this, and why does it behave this way?
source share