Please enable Javascript for better experience...
Difference between Class and Struct in C#
By Rahul Kumar Jha | Nov 30, 2014 | In Articles | Update: Jul 27, 2015 | Total Views [ 3936 ]
Taged In
(4 Like)

Both class and struct have nearly same functionality but there is big difference between both. In this article I will try to explain difference between Class and Struct with an example.


Struct may seem similar to class, but there are many differences that make it alike. One such known difference is that class is reference type whereas structs are value type. There are few more differences which make class and struct alike from each other.

Class Vs. Struct (Structure)

Let see the difference between struct and class. Class is a reference type and struct is value type. It means that if you copy a class to another class, second class still reference first class's address. As a result if you change anything in second class, it will reflect in first class also. Now because struct is value type, repeating same step will create a refresh copy of struct A into struct B and changing struct B values will not reflect in struct A.

ClassStruct (Structure)
Class is reference type. Struct is value type.

Use class keyword to define a class

Eg. class MyClass { }

Use struct keyword to define a struct.

Eg. struct MyStruct { }

All type of constructors are allowed. Cannot create default constructor explicitly in struct. It is automatically provided by compiler and intialise all the fields with default value. You can only create parameterized constructor for struct. Click MSDN for more detail.
Can create desctructor of a class. Defining desctructor is not allowed.
Variable assignment creates reference of class. Variable assignment creates copy of Struct.
A variable of a class type contains a reference to the data. It means, changing value in second object will automatically reflect in first object. A variable of a struct type directly contains the data of the struct. So updating value has no affect at all.

Share this

About the Author

Rahul Kumar Jha
Rahul Kumar Jha
Founder, Developer

Public profile: user/profile/99900001

Has working experience in different phases of Software Development Life Cycle (SDLC) in CMS, Gaming, Health Care and Financial Services domain using Agile pattern. Working experience in Design patterns, ASP.NET, MVC, ANGULAR, ANGULAR JS, Windows application, WCF, ADO.NET, SQL Server and Test Driven Development (TDD) environment with JQuery, JavaScript, N-Unit, Entity Frameworks, LINQ, Code Refactoring and Business Objects Models.

User's Comments

Please SignUp/Login to comment...

Or comment as anonymous...
* Name
* Email ID