Author : Rahul
Last Modified : 22-May-2021
Complexity : Beginner

Structure Vs Class in C#


Class

A Class is a user-defined data type and it is a reference type. It provides a skeleton from which a user can create other objects. In simple words, a class is a combination of Data Members (Properties) and Data Methods (Behaviour) which is put into a single unit.

Example:

using System; 
 
 // Class Declaration 
 public class Employee
 {
   // Data members of class 
   public string Emp_Name;
   public string Emp_Number;
   public int Emp_Salary;
   // Data Methods of class 
   public void Set_Details(string name, string number, int salary)
   {
     this.Emp_Name = name;
     this.Emp_Number = number;
     this.Emp_Salary = salary;
   }
   public Employee Get_Details()
   {
     return this;
   }
 }
 public class Test
 {
   // Main Method 
   public static void Main(String[] args)
   {
     Employee emp = new Employee();
     emp.Set_Details("Rahul", "1001", 10000);
     Employee new_emp = emp.Get_Details();
     Console.WriteLine("Employee Details:");
     Console.WriteLine("Employee Name: " + new_emp.Emp_Name);
     Console.WriteLine("Employee Number: " + new_emp.Emp_Number);
     Console.WriteLine("Employee Salary: " + new_emp.Emp_Salary);
   }
 }

Output:

Employee Details:
Employee Name: Rahul
Employee Number: 1001
Employee Salary: 10000

 

Structure

A Structure is also a user-defined data type but it is a value type. In simple words, a structure is a collection of variables of different data types which is put into a single unit.

Example

using System; 
 
 // Defining structure 
 public struct Phone
 {
   // Declaring different data types 
   public string Brand;
   public string Color;
   public float ScreenSize;
 }
 class Test
 {
   // Main Method 
   static void Main(string[] args)
   {
     // Declare obj of type Phone 
     Phone obj;
     // obj's data 
     obj.Brand = "Samsung";
     obj.Color = "Gray";
     obj.ScreenSize = 6;
     // Displaying the values 
     Console.WriteLine("Phone Details:");
     Console.WriteLine("Brand: " + obj.Brand);
     Console.WriteLine("Color: " + obj.Color);
     Console.WriteLine("Screen Size:" + obj.ScreenSize);
   }
 }

Output

Phone Details:
Brand: Samsung
Color: Gray
Screen Size: 6

The difference between Class and Structure are as follow

Structure

  1. The structure is a value type and it is derived from System.Value Type.
  2. Since it is a value type so data stored on Stack.
  3. A Structure is usually used for small amounts of data.
  4. A Structure does not contain a Default Constructor but can contain Parameterized Constructor or Static Constructor.
  5. It is not required to use a new keyword to create an instance of Structure.
  6. A Structure can not inherit from another structure or class.
  7. Protected access specifier can not be used with data member of Structure.
  8. The function member of structure can’t be virtual or abstract.
  9. Structure can’t be inherited from other types.

Class

  1. The class is a reference type and it is derived from System.Object Type.
  2. Since it is a reference type so data stored on Heap.
  3. A Class is usually used for large amounts of data.
  4. A Class can contain any type of Constructor.
  5. It is required to use a new keyword to create an instance of Class.
  6. A Class can inherit from another class.
  7. The all-access specifier can be used with data member of the Class.
  8. The function member of a class can be virtual or abstract.