Why are we not forced to create a structure using "new", for example, when using a class?
When you are a “new” reference type, three things happen. First, the memory manager allocates space from long-term storage. Secondly, a reference to this space is passed to the constructor, which initializes the instance. Thirdly, this link is passed back to the caller.
When you are a “new” type of meaning, three things happen. First, the memory manager allocates space from short-term storage . Secondly, the constructor receives a link to short-term storage. After starting the constructor, the value that was in the short-term storage is copied to the storage for the value, wherever it is. Remember that value type variables retain the actual value.
(Note that the compiler is allowed to optimize these three steps in one step, if the compiler can determine that it never provides partially constructed structure code for the user. That is, the compiler can generate code that simply passes the link to the final storage location for the constructor, thereby preserving one selection and one copy.)
So, now we can solve your question, which you actually asked back. It would be better to ask:
Why are we forced to allocate a class with "new" instead of just initializing the fields as with a structure?
You must highlight the class with the “new” because of these three things in the list. You need new memory allocated from a long-term storage, and you need to pass a link to this storage to the constructor. “new” is an operator that knows how to do this.
You do not need to call the “new” in the structure, because there is no need to allocate the “final” storage; the final repository already exists . The new value will go somewhere, and you have already received this store in other ways. Value types do not need a new distribution; all they need is initialization. All you have to do is make sure that the repository is properly initialized, and you can often do this without calling the constructor. Of course, this means that you run the risk of having a variable of type value that can be observed in a partially initialized state according to the user code.
To summarize: calling ctor is optional for value types, because when you initialize an instance of a value type, you don’t need to allocate new memory, but because skipping the constructor’s call means you can skip the short-term allocation and copy, the price you pay for this increase performance, lies in the fact that user code can see a partially initialized structure.