What is Struct in C#?

This article describes the basic overview of struct or how to create struct in C# or how to use struct in C#; Smmery of the article:

  • What is Struct?
  • Defining a Struct
  • Example of Struct
  • Properties or Features of Struct
  • Class vs Struct
  • When to use Struct?
  • When not to use Struct?

What is Struct?
In C# struct is a User-Defined Value Types data types. It helps us to keep related data in a single variable. It is used to represent a record. Suppose if we want to keep the records of students we will use the following attributes.
StudentID
StudentName
Address

Defining a Structure
To define a structure we need to use the struct statement.
Example: A struct for the above student:
struct Student
{
public int StudentID;
public string StudentName;
public string Address;

};

Example of Struct
An Example of a C# struct is:

struct Student
{
    public int StudentID;
    public string StudentName;
    public string Address;

};

static void Main(string[] args)
{
    Student oStudent1;
    Student oStudent2;
    oStudent1.StudentID = 1;
    oStudent1.StudentName = "Mr. Mikel Kosta";
    oStudent1.Address = "London";

    oStudent2.StudentID = 2;
    oStudent2.StudentName = "Mr. Kamal Hossain";
    oStudent2.Address = "Dubai";

    // Print Student1 info 
    Console.WriteLine("Student1 ID      : {0}", oStudent1.StudentID);
    Console.WriteLine("Student1 Name    : {0}", oStudent1.StudentName);
    Console.WriteLine("Student1 Address : {0}", oStudent1.Address);

    // Print Student2 info 
    Console.WriteLine("Student2 ID      : {0}", oStudent2.StudentID);
    Console.WriteLine("Student2 Name    : {0}", oStudent2.StudentName);
    Console.WriteLine("Student2 Address : {0}", oStudent2.Address);
    Console.Read();
}

Output:
If we run the above code in a Console Application the output will be:
Student1 ID : 1
Student1 Name : Mr. Mikel Kosta
Student1 Address : London
Student2 ID : 2
Student2 Name : Mr. Kamal Hossain
Student2 Address : Dubai

Properties or Features of Struct
C# struct have the following properties or features:

  • It can have methods, fields, indexers, properties, operator methods, and events.
  • Structures can have defined constructors, but not destructors. The default constructor is automatically defined and can’t be changed.
  • It cannot inherit other structures or classes.
  • It cannot be used as a base for other structures or classes.
  • IN A structure WE can implement one or more interfaces.
  • Structure members cannot be specified as virtual, abstract, and protected.
  • It can be instantiated without using the New operator.

Class vs Struct
The difference between Classes and Structures:

  • Classes are reference types and structs are value types
  • Class support inheritance. Struct do not support inheritance
  • Class cannot be instantiated without New operator. Struct can be instantiated without New operator.

When to use Struct?
We will use struct for the following conditions:

  • Need to create lot of instance and drop them after work. (Inside a loop)
  • You don’t want to derive from other types

When not to use Struct?
We will not use struct for the following conditions:

  • The size of the struct gets large. Microsoft recommends that the size of a struct should ideally be below 16 bytes.
  • Use instance of struct in collection and made modification of the element of collection. Because every modification will arise boxing/unboxing.

That’s all about C# struct.

No Responses

Leave a Reply

Your email address will not be published. Required fields are marked *