Articles → CSHARP → Difference Between Struct And Class In C#

Difference Between Struct And Class In C#






Difference


ClassStruct
Classes in C# support inheritance.Structs do not support inheritance.
The class has a default constructor.No default constructor is defined for the struct.
Classes are stored in a heap because they are reference types.Structs are stored in the stack because they are value types.
The garbage collector automatically frees memory used by class objects when they are no longer in use.Memory allocated to the struct is automatically reclaimed.
When a class object is passed as a parameter to a function, a reference to the object is copied. This means that changes made to the object inside the function will also affect the original object outside the function.When a struct is passed as a parameter to a function, its value is copied. As a result, changes made to the struct inside the function do not affect the original struct outside the function.



Posted By  -  Karan Gupta
 
Posted On  -  Monday, February 27, 2017

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250