In this article, I am going to share information with you about Structures in C#.
What is Structure?
Follow the steps to learn what Structure really is.
The 'Struct' keyword is used to create a structure. A structure can contain variables, methods, static constructors, parameterized constructors, operators, indexers, events, and properties.
The syntax of Structure,
struct Struct_Name
{
// Structure members
}
We have used variables, methods, and properties inside the structure as shown in the below program. Struct members can be static and non-static.
using System;
namespace Tutpoint
{
class Program
{
struct Student
{
// Variables
int Roll_no;
string Name;
string Mobile;
// Enum
enum course { BTech, MBA, BPharma, MA, BSc }
// Static method
public static void marks()
{
Console.WriteLine("Marks");
}
// Method
public void Grades()
{
Console.WriteLine("Grades");
}
// Property
public int Serial_No { get; set; }
}
static void Main(string[] args)
{
Console.ReadKey();
}
}
}
A structure can not derive/inherit from any structure or class.
As shown in the below program when a struct 'Syllabus' tries to inherit/derive a struct 'Student' then the compiler will produce an error as "Type 'Program. Student' in interface list is not an interface". The same error will be produced if the struct tries to inherit/derive a class.
using System;
namespace Tutpoint
{
class Program
{
struct Student
{
// Variables
int Roll_no;
string Name;
string Mobile;
// Enum
enum course { BTech, MBA, BPharma, MA, BSc }
// Static method
public static void marks()
{
Console.WriteLine("Marks");
}
// Method
public void Grades()
{
Console.WriteLine("Grades");
}
// Property
public int Serial_No { get; set; }
}
// When Struct 'Syllabus' try to inherit from Struct 'Student' then compiler will produce an error
// Error as "Type 'Program.Student' in interface list is not an interface"
struct Syllabus : Student
{
int Roll_no;
int status;
}
static void Main(string[] args)
{
Console.WriteLine("December ");
Console.ReadKey();
}
}
}
A Structure can implement any number of interfaces.
In the below program, struct 'Student' implemented 2 interfaces 'IStudent' and 'IRecords'. Method of Interface 'IRecords' is defined inside the struct.
using System;
namespace Tutpoint
{
class Program
{
interface IStudent
{
}
interface IRecords
{
void Records();
}
//struct implements 2 interfaces 'IRecords' and 'IStudent'
struct Student : IStudent, IRecords
{
public int Roll_no;
public string Name;
public string Mobile;
//Method from IRecords interface is defined
public void Records()
{
Console.WriteLine("Records");
}
}
static void Main(string[] args)
{
//struct object is creating
Student student = new Student();
student.Records();
Console.ReadKey();
}
}
}
Output
Records
Constructor
We cannot define the default constructor in the structure. If we try to define then the compiler will produce an error as "Structs cannot contain explicit parameterless constructor". We can define static constructors and parameterized constructors. Have a look at the below program to understand more. If we notice the output of this program then 'Static Constructor' is written on the first line. Why so? A static constructor is called automatically while initializing the struct when an instance is created.
In the Main method, first, we create an object of struct 'Student' as we did for class. In the next line, we get the values of struct members with struct object followed by (.) and struct member name.
using System;
namespace Tutpoint
{
class Program
{
struct Student
{
//Variables
public int Roll_no;
public string Name;
public string Mobile;
//Enum
public enum course { BTech, MBA, BPharma, MA, BSc }
// Static Constructor, valid
static Student()
{
Console.WriteLine("Static Constructor");
}
// Struct does not contain Default Constructor
// It will produce an error as "Structs cannot contain explicit parameterless constructor"
public Student()
{
Console.WriteLine("Default Constructor");
}
// Parameterised Constructor
// Valid, This constructor should return all the values of the struct members and must contain all the arguments
public Student(int roll, string name, string mobile, int serial)
{
Roll_no = roll;
Name = name;
Mobile = mobile;
Serial_No = serial;
}
//Property
public int Serial_No { get; set; }
}
static void Main(string[] args)
{
// Creating object of struct 'Student' by passing parameters to it
Student student = new Student(3, "Shubham", "9821705378", 1);
//Struct values are obtained with struct object followed by (.) and struct member name
Console.WriteLine("Roll no: " + student.Roll_no + " Name: " + student.Name + " Mobile no: " + student.Mobile + " Serial no: " + student.Serial_No);
Console.ReadKey();
}
}
}

Ham BagPosted Feb 20, 2023, 6:25 PM
Thanks for this article
Bohdan StupakPosted Jul 6, 2019, 3:33 PM
Pretty exhaustive article. Congratulations
Laxmidhar SahooPosted Nov 25, 2017, 4:35 AM
A good article and impressive