1.

Difference between a class and structure in C#

Answer»

A class is a DATA structure in C# that combines data variables as well as functions into a single unit. Instances of the class are known as objects. The keyword class is used to create the classes.

A structure is a data structure in C# that combines different data variables into a single unit. The keyword struct is used to create the structures. Structures are similar to classes but can be called as lighter versions of them.

Some of the differences between classes and structures are given as follows:

Classes
Structures
Classes are data structures that combine data variables as well as functions into a single unit.
Structures are data structures that combine different data variables into a single unit.
There is support for inheritance in classes.
There is no support for inheritance in structures.
Classes support constructors and can have default constructors, parameterized constructors, copy constructors etc
Structures have default and non PARAMETER constructors that can be replaced by the user
Classes are STORED on a heap.
Structures are EITHER stored in a stack or they are inline.
Classes require the new OPERATOR for instantiation.
Structurescan be instantiated without using the new operator.
Classes are a data type in C# that are of reference type.
Structures are a data type in C# that are of value type.
Classes are used for complex data or data that is intended to be modified after the class is created.
Structures are used for small data that is not intended to be modified after the structure is created.
There are abstract classes that are used in inheritance.
There cannot be abstract structures.


Discussion

No Comment Found