Destructor is used to destruct instances of classes and relase the resources. When you create new object of any class it will allocate memory on the heap.
when you create struct, it will create on stack memory because struct is value type. struct do not directly dealing with reference to any instance so struct have nothing to remove or destroy on Destructor. so Destructor is not supported with struct.
Struct is not support parameterless Constructor because struct is value type, compilers by default neither generate a default constructor, nor do they generate a call to the default constructor for value type.
Unlike classes, it is not necessary to use 'new' to create an instance of a struct.
If you don't use 'new', then an instance of the struct is created whose fields are all set to their default values (0 for numbers, false for bool, '\0' for char and null for reference types).
If you had been allowed to create your own parameterless constructor for structs, then fields could have been set to something other than their default values.
The C# design team felt that this was a potentially confusing situation and so disallowed it.
In classes, destructors (where they exist) are always called automatically by the garbage collector prior to the object's memory being reclaimed.
As structs are not subject to garbage collection, destructors would have been pointless and so are not allowed.
Jignesh TrivediPosted Apr 22, 2014, 5:30 AM
Destructor is used to destruct instances of classes and relase the resources. When you create new object of any class it will allocate memory on the heap.
when you create struct, it will create on stack memory because struct is value type. struct do not directly dealing with reference to any instance so struct have nothing to remove or destroy on Destructor. so Destructor is not supported with struct.
Struct is not support parameterless Constructor because struct is value type, compilers by default neither generate a default constructor, nor do they generate a call to the default constructor for value type.
VulpesPosted Apr 22, 2014, 5:15 AM