Sunday, April 15, 2012

Differentiate between Structure and Class in .net?

                        Structure

                           Class

Structures are value type, i.e. variable of struct type directly contain the data of structure

Classes are reference type, i.e variable of class (known as object) contain the reference to data.

Do not require, urgently, new operator to do its instantiation. Or instantiation can be done without new operator.

Do require new operator to do instantiation.

Each variable will have its own set of data even if they have same values.

More than one object can have reference to same data.

Default Value of a struct type can’t be null.

Default value of an Object of a class type is null.

All struct types are implicitly inherited from class System.ValueType

All class types are implicitly inherited from

System.Object class.

Struct type do not support inheritance.

Class types support inheritance.

Struct types are implicitly sealed.

Class types are implicitly internal.

Protected, sealed, protected Internal, abstract access modifiers are  not valid for members of structure. It support only public, private and internal.

It supports all modifiers for its members.

A structure does not allow parameter less constructor declaration.

It allows declaration of parameter less constructor.

It doesn’t allow the declaration of destructor inside.

It allows declaration of destructor inside.

Struct can not be inherited from a class or struct.

Class can be inherited from a class or struct.

By default members are public.

By default members are private.

Structure use stack for storage.

Class use heap for storage.

Garbage collection does not operate on structure.

Garbage collection operates on class.

No comments:

Post a Comment