Difference Between Structure and Class?
Structure |
Class |
---|---|
Structure is a Value type and stored in stack. |
Class is a reference type and stored in heap. |
Structures do not support inheritance. |
Classes support inheritance. |
By default all members of Structure are public. |
In case of Class, all constants and variables are private while other members of the Class are public by default. |
Structure members can't be declared Protected. |
Class members can be declared Protected. |
A Structure cannot be fully empty. |
A Class can be fully empty. |
A Structure does not need a constructor. |
A Class needs a constructor. |
Finalize method is never called for Structures as they are never terminated. |
Finalize method is called for Classes as they are terminated by GC (Garbage collector) that calls the Finalize method. |
Even after assigning the value nothing to a Structure variable, instance continues to be associated with the variable and we can access its data members. |
In Class, if we assign nothing to object variable we cannot access any member through the variable until you assign another instance to it. |
Boxing of a Structure object creates a copy of the object in a different type. |
Boxing a Class object creates a reference (of different type) to the same object. |
Structure are value type it is not possible to assign null to a Structure object. |
Classes are reference type so it is possible to assign null value to Class object. |
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.