Structure's

Structure's

  • This concept is not new in C#; it is taken from C language.
  • In C language‘s structures, you can write only some member variables, called as data members / fields. But in C#‘s structures, you can write fields along with some methods also.
  • So, In C#, structures are almost all similar to the classes, but having some differences with the classes.

Difference Between Classes and Structures

Classes Structures
Class is a references type data type Struct is a value type data type
When we create an object for class it will be allocated memory in Heap Memory. When we create an object for the structure it will be allocated memory in Stack Memory.
To define a class, we need to use the class keyword. To define a structure, we need to use the structure keyword.
class will support inheritance structure will not support inheritance
Instance field initialisation is allowed in class. Instance field initialisation is not allowed in the structure.
A class can have an explicit default constructor A structure can't have an explicit default constructor
We can't create an object for a class without using a new keyword. We can create an object for a structure without using a new keyword if it does not have an instance variable.
We can have static, abstract and sealed classes We can't have static, abstract and sealed structures.
We can implement overloading and overriding within the class. We can implement overloading within a structure, but we can't implement overriding within a structure.

Syntax :

  1. Structure syntax
  2. 
    struct structurename
     {
    	//fields
    	//methods
     }
    
  3. Create instance for structure
  4. structurename instancename = new structurename();

C# Structures Coding:

we just looking into the overview of the structure skeleton syntax


namespace StructuresDemo
{
 struct Employee
 {
	//fields declare here
  
	//methods
	public void ReadData() {	//code	}

	public void Calculate() {	//code	}

	public void DisplayData() {	//code	}
 }
}

I'm defining a few fields for employee structure as shown in the below code snippet to do some operations in the methods that we declared in the employee structure class.


//fields
private int EmployeeID;
private string EmployeeName;
private double Salary;
private double Tax;
private double NetSalary;

The ReadData() method is used to accept values from the command prompt and store them in corresponding fields and these fields we use for further operations.


//methods
public void ReadData()
 {
	Console.Write("Enter Employee ID: ");
	EmployeeID = Convert.ToInt32(Console.ReadLine());
	Console.Write("Enter Employee Name: ");
	EmployeeName = Console.ReadLine();
	Console.Write("Enter Salary: ");
	Salary = Convert.ToDouble(Console.ReadLine());
 }

The Calculate() method is used to calculate tax based on a salary for which we passed the salary amount in the ReadData() method and also calculate net salary from base salary and tax.


public void Calculate()
 {
	Tax = Salary * 10 / 100;
	NetSalary = Salary - Tax;
 }

The DisplayData() method is used to retrieve the data from the fields and display it to the user.


public void DisplayData()
 {
	Console.WriteLine("\n\nEmployee ID: " + EmployeeID);
	Console.WriteLine("Employee Name: " + EmployeeName);
	Console.WriteLine("Salary: " + Salary);
	Console.WriteLine("Tax: " + Tax);
	Console.WriteLine("Net Salary: " + NetSalary);
 }

In static void main method we create an object for the Employee structure and call the method through structure instance.


namespace StructuresDemo
{
 class Program
 {
  static void Main(string[] args)
  {
	//create structure instance
	Employee e = new Employee();    
	//call the methods
	e.ReadData();
	e.Calculate();
	e.DisplayData();
	Console.ReadLine();
  }
 }
}

You can check out the same code from the article in the Fiddle provided below. Feel free to explore and experiment with it directly in the Fiddle interface.

Post a Comment

0 Comments