Following are the main points of difference between classes and structs in C#:
- Structs are value type and classes are reference type.
- Whenever a struct object is assigned to another struct object a copy is created. In case of classes the same operation results in two references referring to same object: For example, a.x = 10; b = a; a.x = 20; cout << b.x will display '10' if a and b are struct objects and it will display '20' if a and b are class objects.
- Boxing of a struct object creates a copy of the object in a different type, whereas, boxing a class object creates a reference (of different type) to the same object.
- Inheritance is
Login to rate this answer.
Following are the main points of difference between classes and structs in C#:
- Value type vs Reference type: Structs are value type and classes are reference type.
- Whenever a struct object is assigned to another struct object a copy is created. In case of classes the same operation results in two references referring to same object: For example, a.x = 10; b = a; a.x = 20; cout << b.x will display '10' if a and b are struct objects and it will display '20' if a and b are class objects.
- Boxing of a struct object creates a copy of the object in a different type, whereas, boxing a class object creates a reference (of different type) to the same object.
- Since structs are value type it is not possible to assign null to a struct object.
- When a struct object is passed to a function as a parameter it is passed by value, unless specifically marked as 'ref' or 'out' parameter.
- Inheritance: Inheritance is not allowed for structs.
- All struct types implicitly inherit from System.ValueType and they are implicitly sealed.
- The keywords related to inheritance concepts are not allowed for structs. For example, abstract, protected, virtual, override etc.
- Initialization: Structs cannot have default constructor.
- All struct member variables of value type are initialized to their default values and reference member variables are initialized to null.
- Field initializers (initialization in the member variable declaration line like int a = 0; ) is also not allowed for structs.
- Destructor: Structs cannot declare a destructor.
- Meaning of 'this':
- For a struct instance constructor (parameterized) 'this' is treated as an out parameter.
- For other member functions it is treated as a 'ref' parameter.
- So it is possible to assign to 'this' and change the current object.
- In case of a class 'this' is considered as a value, so it is not possible to assign to 'this' from inside a class.
- Since 'this' is a 'ref' or 'out' parameter, it must be definitely assigned at every point where it is used.

4 Users have rated as useful.
Login to rate this answer.
Class Struct
1.This is reference Type 1.This is Value Type
2.It Supports Inheritence 2.It Doesnt Supports Inheritence
3. It Have Default Constructor 3.It Doesnt Have any Default Constructors
4.Object can be created by using 4.We can create object without using "new"
"new" Keyword
5.It is more useful in collections 5. It is more useful in Arrays
Login to rate this answer.