In C#, a structure is a value type that can encapsulate data and related functionality. It is declared using the struct
keyword.
The general format of a structure declaration is as follows.
<AccessModifiers> struct <StructName>
{
//Body
}
internal
is the default.struct
is the required keyword.Let's consider the following simple example where we have declared a structure named Point3D
which contains three integers X
, Y
, and Z
.
public struct Point3D
{
public int X;
public int Y;
public int Z;
}
Structures are also used to represent a record. For example, if you want to keep track of all your customers, you can add the following fields to the structure.
public struct Customer
{
public int Id;
public string Name;
public string Address;
}
To use the structure, you can create its object using with or without the new
keyword.
Point3D point1;
Point3D point2 = new Point3D();
point1.X = 10;
point1.Y = 20;
point1.Z = 30;
point2.X = 40;
point2.Y = 50;
point2.Z = 60;
Console.WriteLine("X: {0}, Y: {1}, Z: {2}", point1.X, point1.Y, point1.Z);
Console.WriteLine("X: {0}, Y: {1}, Z: {2}", point2.X, point2.Y, point2.Z);
Let's run the above code and it will print the following output on the console window.
X: 10, Y: 20, Z: 30
X: 40, Y: 50, Z: 60
A struct
only supports constructors which contain parameters. Here in the Customer structure, a parametrized constructor is added.
public struct Customer
{
public int Id;
public string Name;
public string Address;
public Customer(int id, string name, string address)
{
this.Id = id;
this.Name = name;
this.Address = address;
}
}
You can create its object by calling the parameterized constructor.
Customer customer = new Customer(1, "Mark", "22 wall street");
Console.WriteLine("Id: {0}, Name: {1}, Address: {2}", customer.Id, customer.Name, customer.Address);
Let's run the above code and it will print the following output on the console window.
Id: 1, Name: Mark, Address: 22 wall street
You can also add methods to your structure in C#. Here is a simple method Print()
which will print all the information of a customer on the console window.
public struct Customer
{
public int Id;
public string Name;
public string Address;
public Customer(int id, string name, string address)
{
this.Id = id;
this.Name = name;
this.Address = address;
}
public void Print()
{
Console.WriteLine("Id: {0}, Name: {1}, Address: {2}", this.Id, this.Name, this.Address);
}
}
Now to print the customer information, we just need to call the Print()
method.
Customer customer = new Customer(2, "John", "11 wall street");
customer.Print();
Let's run the above code and it will print the following output on the console window.
Id: 2, Name: John, Address: 11 wall street
When you are working with structures, you must keep in mind the following limitations.
All the examples related to the structure are available in the Structure.cs
file of the source code. Download the source code and try out all the examples for better understanding.