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
- The structure is a value type and it is derived from System.Value Type.
- Since it is a value type so data stored on Stack.
- A Structure is usually used for small amounts of data.
- A Structure does not contain a Default Constructor but can contain Parameterized Constructor or Static Constructor.
- It is not required to use a new keyword to create an instance of Structure.
- A Structure can not inherit from another structure or class.
- Protected access specifier can not be used with data member of Structure.
- The function member of structure can’t be virtual or abstract.
- Structure can’t be inherited from other types.
Class
- The class is a reference type and it is derived from System.Object Type.
- Since it is a reference type so data stored on Heap.
- A Class is usually used for large amounts of data.
- A Class can contain any type of Constructor.
- It is required to use a new keyword to create an instance of Class.
- A Class can inherit from another class.
- The all-access specifier can be used with data member of the Class.
- The function member of a class can be virtual or abstract.