C# Structure with Examples

In C #, the structure is determined by the keyword struct. The keyword Struct allows you to define structures that contain different types of data. The structure can also include constructors, constants, fields, methods, properties, indexers, events, and more.

Syntax:

Access_Modifier struct structure_name
{     
     // Block of code
}

A structure is a type of value and a set of variables of different data types in a block. It almost looks like a class because they are both custom data types and both contain many different data types. C # allows you to use predefined data types. However, the user sometimes wants to define their own data types, also known as custom data types. Although it belongs to a value type, it can be changed as needed and is therefore also called a custom data type.

Example:

    public struct Student
    {
        public string Name;

    }

    class Tech
    {
        static void Main(string[] args)
        {
            Student student;
            student.Name = "shahzad";
            Console.WriteLine(student.Name);
        }
    }

Output:

shahzad

Types of structure

  • Copy Structure
  • Nesting of Structure

Copy Structure

In C # the user can copy one structure object to another with the "=" (Assignment) operator.

Syntax of Copy Structure:

Structure_object = structure_object;

Example:

 public struct Student
    {
        public string Name;

    }

    class Tech
    {
        static void Main(string[] args)
        {
            Student student;
            Student student1;
            student.Name = "shahzad";
            student1 = student;
            Console.WriteLine(student.Name);
            Console.WriteLine(student1.Name);
        }
    }

Output:

shahzad
shahzad

Data members of the student structure begin to use student, and data member values ​​can be copied from student to student1 using "=" (assignment operator).

Nesting of Structure

C # allows one structure to be reported to another structure, and this concept is called nesting structure.

Example:

public struct Rollnumber
    {
        public int RNumber;
    }
    public struct Student
    {
        public string Name;
       public Rollnumber rollnumber;
    }

    class Tech
    {
        static void Main(string[] args)
        {
            Student student;
            student.Name = "shahzad";
            student.rollnumber.RNumber = 15;
            Console.WriteLine(student.Name);
            Console.WriteLine(student.rollnumber.RNumber);
        }
    }

Output:

shahzad
15

Important points about constructions:

  • When structures are out of range, they are automatically rolled back.
  • It is much faster than heap types.
  • The structure makes it easy to copy the values ​​of variables to the stack.

Difference between Class and Structure

Class Structure
Classes are a kind of reference value type
All reference types are arranged in a smaller memory stack memory used to assign the value type.
The class has unlimited functionality Struct has limited functionality.
commonly used in large programs Used in the small program
Classes can be constructive or destructive A structure does not have a constructor or destroyer without parameters