Difference between class and Struct

Class Struct
Class is a Reference Type – Structure is Value type
When the object reference is assigned to a new variable, the new variable refers to the original object. Changes made through one variable are reflected in the other variable because they both refer to the same data. When the struct is assigned to a new variable, it is copied. The new variable and the original variable therefore contain two separate copies of the same data. Changes made to one copy do not affect the other copy.
In general, classes are used to model more complex behavior, or data that is intended to be modified after a class object is created Structs are best suited for small data structures that contain primarily data that is not intended to be modified after the struct is created
Comes into the inheritance chain Doesn’t come in inheritance chain
All the members of a Class are private by default All the members of a Struct are public by default
   
As it uses pass by reference, garbage collection is possible and hence garbage collection process helps in effective memory management As it uses pass by value, garbage collection is not possible and hence The lack of garbage collection results in poor memory management
Can use destructor Doesn’t have destructor
Must to use new keyword during the object creation It is optional to use new keyword.
A Class can declare a default constructor (a constructor without parameters) or a finalizer A struct cannot declare a default constructor (a constructor without parameters) or a finalizer

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s