Structures

 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”. But in C#’s structures, you can write data members along with some methods also.
 In C#, structures are almost all similar to the classes, but having some differences with the classes.

Structures (vs) Classes

Sl. No Classes Structures
1 Declared with “class” keyword. Declared with “struct” keyword.
2 Can contain data members, methods, constructors, destructors, properties etc. Can contain data members, methods, constructors, destructors, properties etc.
3 “new” keyword is required to create an instance. “new” keyword is not required to create an instance.
4 Supports inheritance. Doesn’t supports inheritance.
5 User-defined default constructor can be implemented. User-defined default constructor can’t be implemented.
6 Data Members can be initialized in the class definition. Data members can’t be initialized in the structure definition.

Implementation:

 Define structure

struct structurename
{
//data members
//methods
}
 Create instance for structure

structurename instancename;

Demo on Structures

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StructuresDemo
{
struct Employee
{
public int EmployeeID;
public string EmployeeName;
}

class Program
{
static void Main(string[] args)
{
Employee e;
e.EmployeeID = 101;
e.EmployeeName = "Laxman";
Console.WriteLine(e.EmployeeID);
Console.WriteLine(e.EmployeeName);
Console.Read();
}
}
}
Structures