C# - Structures



In C#, a structure (struct) is a value type. It helps us to make a single variable that hold related variables of various data types. The struct keyword is used for creating a structure.

Structures are used to represent a record. Suppose you want to keep track of your books in a library. You might want to track the following attributes about each book −

  • Title
  • Author
  • Subject
  • Book ID
The structures are similar to the classes, but the only difference is that the structures are stored in a stack while the classes are stored in a heap.

Defining a Structure

To define a structure, you must use the struct keyword. The struct keyword defines a new data type, with more than one member for your program.

For example, here is the way you can declare the Book structure −

struct Books {
   public string title;
   public string author;
   public string subject;
   public int book_id;
};  

Example of C# Structure

The following program shows the use of the structure −

using System;
struct Books {
   public string title;
   public string author;
   public string subject;
   public int book_id;
};  

public class testStructure {
   public static void Main(string[] args) {
      Books Book1;   /* Declare Book1 of type Book */
      Books Book2;   /* Declare Book2 of type Book */

      /* book 1 specification */
      Book1.title = "C Programming";
      Book1.author = "Nuha Ali"; 
      Book1.subject = "C Programming Tutorial";
      Book1.book_id = 6495407;

      /* book 2 specification */
      Book2.title = "Telecom Billing";
      Book2.author = "Zara Ali";
      Book2.subject =  "Telecom Billing Tutorial";
      Book2.book_id = 6495700;

      /* print Book1 info */
      Console.WriteLine( "Book 1 title : {0}", Book1.title);
      Console.WriteLine("Book 1 author : {0}", Book1.author);
      Console.WriteLine("Book 1 subject : {0}", Book1.subject);
      Console.WriteLine("Book 1 book_id :{0}", Book1.book_id);

      /* print Book2 info */
      Console.WriteLine("Book 2 title : {0}", Book2.title);
      Console.WriteLine("Book 2 author : {0}", Book2.author);
      Console.WriteLine("Book 2 subject : {0}", Book2.subject);
      Console.WriteLine("Book 2 book_id : {0}", Book2.book_id);       

      Console.ReadKey();
   }
}

When the above code is compiled and executed, it produces the following result −

Book 1 title : C Programming
Book 1 author : Nuha Ali
Book 1 subject : C Programming Tutorial
Book 1 book_id : 6495407
Book 2 title : Telecom Billing
Book 2 author : Zara Ali
Book 2 subject : Telecom Billing Tutorial
Book 2 book_id : 6495700

Initializing a Structure

There can be multiple ways to initialize a structure in C#:

1. Using a Parameterized Constructor

You can initialize a structure using the parametrized constructors while creating an object of a structure.

using System;

struct Person {
    public string name;
    public int age;
    public string empid;

    // Parameterized Constructor
    public Person(string name, int age, string empid) {
        this.name = name;
        this.age = age;
        this.empid = empid;
    }
}

class Program {
    static void Main() {
        Person p1 = new Person("Sudhir Sharma", 27, "SEO01");
        Console.WriteLine($"Name: {p1.name}, Age: {p1.age}, Employee ID: {p1.empid}");
    }
}

When the above code is compiled and executed, it produces the following result −

Name: Sudhir Sharma, Age: 27, Employee ID: SEO01

2. Using Default Initialization ('new` Keyword)

If you want to provide the default values to the structure fields (e.g., 0 for numbers, null for nullable types, empty for strings), you can use the "new" keyword.

using System;

struct Person {
    public string name;
    public int age;
    public string empid;
}

class Program {
    static void Main() {
        Person p1 = new Person(); // Default initialization
        p1.name = "Sudhir Sharma";
        p1.age = 27;
        p1.empid = "SEO01";

        Console.WriteLine($"Name: {p1.name}, Age: {p1.age}, Employee ID: {p1.empid}");
    }
}

When the above code is compiled and executed, it produces the following result −

Name: Sudhir Sharma, Age: 27, Employee ID: SEO01

3. Without Using 'new' (Manually Assigning Values)

Since, C# structs do not require the new keyword to be instantiated. You can also initialize all fields before the use.

using System;

struct Person {
    public string name;
    public int age;
    public string empid;
}

class Program {
    static void Main() {
        Person p1;
        p1.name = "Sudhir Sharma"; 
        p1.age = 27;
        p1.empid = "SEO01";

        Console.WriteLine($"Name: {p1.name}, Age: {p1.age}, Employee ID: {p1.empid}");
    }
}

When the above code is compiled and executed, it produces the following result −

Name: Sudhir Sharma, Age: 27, Employee ID: SEO01

Immutable Structures (structs)

You can make struct fields immutable (read-only) by using the readonly keyword.

Example

In the following example, we are defining a struct Person with three readonly fields: name, age, and empid.

using System;

struct Person {
    public readonly string name;
    public readonly int age;
    public readonly string empid;

    public Person(string name, int age, string empid) {
        this.name = name;
        this.age = age;
        this.empid = empid;
    }

    public void Display() {
        Console.WriteLine($"Name: {name}, Age: {age}, Employee ID: {empid}");
    }
}

class Program {
    static void Main() {
        Person p1 = new Person("Sudhir Sharma", 27, "SEO01");
        p1.Display();
    }
}

When the above code is compiled and executed, it produces the following result −

Name: Sudhir Sharma, Age: 27, Employee ID: SEO01

Features of C# Structures

You have already used a simple structure named Books. Structures in C# are quite different from that in traditional C or C++. The C# structures have the following features −

  • Structures can have methods, fields, indexers, properties, operator methods, and events.

  • Structures can have defined constructors, but not destructors. However, you cannot define a default constructor for a structure. The default constructor is automatically defined and cannot be changed.

  • Unlike classes, structures cannot inherit other structures or classes.

  • Structures cannot be used as a base for other structures or classes.

  • A structure can implement one or more interfaces.

  • Structure members cannot be specified as abstract, virtual, or protected.

  • When you create a struct object using the New operator, it gets created and the appropriate constructor is called. Unlike classes, structs can be instantiated without using the New operator.

  • If the New operator is not used, the fields remain unassigned and the object cannot be used until all the fields are initialized.

Class versus Structure

Classes and Structures have the following basic differences −

  • classes are reference types and structs are value types
  • structures do not support inheritance
  • structures cannot have default constructor

Example

In light of the above discussions, let us rewrite the previous example, where we used the user-defined method getValues and display to get the values and display them respectively −

using System;
struct Books {
   private string title;
   private string author;
   private string subject;
   private int book_id;
   
   public void getValues(string t, string a, string s, int id) {
      title = t;
      author = a;
      subject = s;
      book_id = id;
   }
   
   public void display() {
      Console.WriteLine("Title : {0}", title);
      Console.WriteLine("Author : {0}", author);
      Console.WriteLine("Subject : {0}", subject);
      Console.WriteLine("Book_id :{0}", book_id);
   }
};  

public class testStructure {
   public static void Main(string[] args) {
      Books Book1 = new Books();   /* Declare Book1 of type Book */
      Books Book2 = new Books();   /* Declare Book2 of type Book */

      /* book 1 specification */
      Book1.getValues("C Programming",
      "Nuha Ali", "C Programming Tutorial",6495407);

      /* book 2 specification */
      Book2.getValues("Telecom Billing",
      "Zara Ali", "Telecom Billing Tutorial", 6495700);

      /* print Book1 info */
      Book1.display();

      /* print Book2 info */
      Book2.display(); 

      Console.ReadKey();
   }
}

When the above code is compiled and executed, it produces the following result −

Title : C Programming
Author : Nuha Ali
Subject : C Programming Tutorial
Book_id : 6495407
Title : Telecom Billing
Author : Zara Ali
Subject : Telecom Billing Tutorial
Book_id : 6495700

Structure with Methods and Interfaces

Interfaces define properties, methods, and events, which are the members of the interface. Interfaces contain only the declaration of the members. It is the responsibility of the deriving class to define the members. Interfaces can be implemented using the structures in C#.

Example

The following example demonstrates how to implement using structure with methods and interfaces:

using System;

interface IPerson {
    string GetDetails();
}

struct Person : IPerson {
    public string name;
    public int age;
    public string empid;

    // Constructor to initialize fields
    public Person(string name, int age, string empid) {
        this.name = name;
        this.age = age;
        this.empid = empid;
    }

    // Implementing the interface method
    public string GetDetails() => $"Name: {name}, Age: {age}, Employee ID: {empid}";
}

class Program {
    static void Main() {
        // Creating an instance of Person
        Person p1 = new Person("Sudhir Sharma", 27, "SEO01");
        Console.WriteLine(p1.GetDetails());
    }
}

When the above code is compiled and executed, it produces the following result −

Name: Sudhir Sharma, Age: 27, Employee ID: SEO01
Advertisements