A structure is similar to a class but is a value type, unlike a class which is a reference type. Allocations and deallocations of a value type are chepaer than that of a reference type. So, we can use a structure when we need to change values frequently like in a game. However, when we want to copy data from one instance to another, reference type is a good choice because it will just copy the reference (address) of the instance (shallow copy) whereas, in value type, values of each variable will be copied.
We define a structure using struct
keyword. It is similar to a class and is also defined similarly and can have constructor, methods, fields, properties, etc. But we must have parametrized constructor in a struct, it doesn't support parameterless constructor. Also, we can have a virtual or abstract method. Struct also doesn't support inheritance but it can implement interfaces.
We can initiate a struct with our without the new
keyword. However, in case of not using the new
keyword, the constructor of the struct will not be called and thus the fields will not be initialized. So, we need to initialize each field in this case.
Let's take an example of using the new
keyword first.
using System;
struct Student
{
public int rollNumber;
public string name;
public Student(int r, string n)
{
rollNumber = r;
name = n;
}
}
class Test
{
static void Main(string[] args)
{
Student s = new Student(1, "xyz");
Console.WriteLine(s.rollNumber);
Console.WriteLine(s.name);
}
}
It is similar to a class. Let's take an example of initializing a struct without using the new
keyword.
using System;
struct Student
{
public int rollNumber;
public string name;
}
class Test
{
static void Main(string[] args)
{
Student s;
s.rollNumber = 1;
s.name = "xyz";
Console.WriteLine(s.rollNumber);
Console.WriteLine(s.name);
}
}
In this case, we explicitly gave values to the variables rollNumber and name. Take a note that if any of the variables is not initialized and if we try to access them, we would get an error.
Properties in Struct
We can also have properties in a struct. Let's take an example.
using System;
struct Student
{
public int RollNumber{get; set;}
public string Name{get; set;}
}
class Test
{
static void Main(string[] args)
{
Student s;
s.RollNumber = 1;
s.Name = "xyz";
Console.WriteLine(s.RollNumber);
Console.WriteLine(s.Name);
}
}
Static Constructor in Struct
We can also have a static constructor in a struct.
using System;
struct Student
{
public int rollNumber;
public string name;
static Student()
{
Console.WriteLine("Object 1");
}
public Student(int r, string n)
{
rollNumber = r;
name = n;
}
}
class Test
{
static void Main(string[] args)
{
Student s = new Student(1, "xyz");
Console.WriteLine(s.rollNumber);
Console.WriteLine(s.name);
}
}
Methods in Struct
We can have methods inside a struct. Let's take an example.
using System;
struct Student
{
public int rollNumber;
public string name;
public Student(int r, string n)
{
rollNumber = r;
name = n;
}
public void Display()
{
Console.WriteLine($"Roll Number - {this.rollNumber}\nName - {this.name}");
}
}
class Test
{
static void Main(string[] args)
{
Student s = new Student(1, "xyz");
s.Display();
}
}