2

Possible Duplicate:
Why structs cannot have destructors?

I know that structs in .NET do not have destructor, but its not exactly clear why they are limited in this way.

What technical reason that prevents structs to have destructor?

Community
  • 1
  • 1
Sujit
  • 3,677
  • 9
  • 41
  • 50
  • Structs are never Garbage-collected. A dtor would never be called. So it was made illegal. – H H Jun 28 '12 at 09:30
  • 2
    @HenkHolterman , structs are valuetype, and value-type variables are not garbage-collected, they just fall off the stack when they fall out of scope. Is this the reason structs doesn't have destructor? – Sujit Jun 28 '12 at 09:40
  • `they just fall off the stack when they fall out of scope` Structs are not always on the stack. – mjwills Jun 17 '19 at 09:39

1 Answers1

4

Source:

A struct cannot have a destructor. A destructor is just an override of object.Finalize in disguise, and structs, being value types, are not subject to garbage collection

Also See Destructors

Destructors cannot be defined in structs. They are only used with classes.

The destructor implicitly calls Finalize on the object's base class.

Community
  • 1
  • 1
Habib
  • 219,104
  • 29
  • 407
  • 436