In the structure, the new keyword is uselessly confusing. He does not do anything. This is just required if you want to use the constructor. It does not execute new .
The usual new value is to allocate persistent storage (on the heap). A language like C ++ allows new myObject() or just myObject() . Both calls to the same constructor. But the first creates a new object and returns a pointer. The latter just creates a pace. Any structure or class can use either. new is a choice, and it means something.
C # doesn't give you a choice. Classes are always on the heap, and structures are always on the stack. Unable to execute real new in structure. For this, experienced programmers in C # are used. When they see ms = new MyStruct(); , they know they are ignoring new as just syntax. They know that it acts as ms = MyStruct() , which simply assigns an existing object.
Strange (?), Classes require new . c=myClass(); not allowed (using the constructor to set the values ββof an existing object c .) You need to do something like c.init(); . Therefore, you really have no choice - constructors always allocate for classes, and never for structures. new always just a decoration.
I assume the reason for using fake new in structs is that you can easily change the structure into a class (if you always use myStruct=new myStruct(); on the first declaration, which is recommended).
Owen Reynolds Dec 28 '15 at 23:02 2015-12-28 23:02
source share