What is a structure


C# - Struct

We have learned class in the previous section. Class is a reference type. C# includes a value type entity same as a class called "structure". Structs are mainly useful to hold small data values. A structure can be defined using the struct operator. It can contain a parameterized constructor, static constructor, constants, fields, methods, properties, indexers, operators, events and nested types.

Structure Declaration

A structure is declared using struct keyword with public or internal modifier. The default modifier is internal for the struct and its members. However, you can use private or protected modifier when declared inside a class.
The following declares the simple structure that holds data for employees.

Example: Structure
  1. struct Employee
    {
        public int EmpId;
        public string FirstName;
        public string LastName;
    }

A struct object can be created with or without the new operator, same as primitive type variables. When you create a struct object using the operator, an appropriate constructor is called.
Example: Create struct object using new keyword
  1. struct Employee
    {
        public int EmpId;
        public string FirstName;
        public string LastName;
    }
    
    Employee emp = new Employee();
    Console.Write(emp.EmpId); // prints 0  

In the above code, an object of the structure Employee is created using the new keyword. So, this calls the default parameterless constructor that initializes all the members to their default value.
When you create a structure object without using new keyword, it does not call any constructor and so all the members remain unassigned. So, you must assign values to each member before accessing them, otherwise, it will give a compile-time error.

Example: Create struct object without using new keyword

  1. struct Employee
    {
        public int EmpId;
        public string FirstName;
        public string LastName;
    }
    
    Employee emp;
    Console.Write(emp.EmpId); // Compile time error  
    
    emp.EmpId = 1;
    Console.Write(emp.EmpId); // prints 1  

Constructors in Struct

A struct cannot contain parameterless constructor. It can only contain parameterized constructors or a static constructor. You can declare parameterized constructor to initialize struct members, as shown below.
Example: Parameterized Constructor in Struct

  1. struct Employee
    {
        public int EmpId;
        public string FirstName;
        public string LastName;
    
        public Employee(int empid, string fname, string lname)
        {
            EmpId = empid;
            FirstName = fname;
            LastName = lname;
        }
    }
    
    Employee emp = new Employee(10, "Bill", "Gates");
    
    Console.Write(emp.EmpId); // prints 10  
    Console.Write(emp.FirstName); // prints Bill  
    Console.Write(emp.LastName); // prints Gates  

Please note that you must assign values to all the members of a struct in parameterized constructor, otherwise it will give compile-time error if any member remains unassigned.
A struct can include static parameterless constructor and static fields.
Example: Static Constructor in Struct

  1. struct Employee
    {
        public int EmpId;
        public string FirstName;
        public string LastName;
    
        static Employee()
        {
            Console.Write("First object created");
        }
    
        public Employee(int empid, string fname, string lname)
        {
            EmpId = empid;
            FirstName = fname;
            LastName = lname;
        }
    }
    
    Employee emp1 = new Employee(10, "Bill", "Gates");
    Employee emp2 = new Employee(10, "Steve", "Jobs");

Methods and Properties in Struct

The structure can contain properties, auto-properties, or methods, same as class.
Example: Methods and Properties in Struct

  1. struct Employee
    {
        public int EmpId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    
        public Employee(int empid, string fname, string lname)
        {
            EmpId = empid;
            FirstName = fname;
            LastName = lname;
        }
    
        public string GetFullName()
        {
            return FirstName + " " + LastName;
        }
    }
    
    Employee emp = new Employee(10, "Bill", "Gates");
    
    Console.Write(emp.GetFullName()); // prints Bill Gates 

Events in Struct

A struct can contain events to notify subscriber about some action. Consider the following example.

  1. struct Point
    {
        private int _x, _y;
    
        public int X 
        {
            get 
            {
                return _x;
            }
    
            set 
            {
                _x = value;
                PointChanged(_x);
            }
        }
    
        public int Y
        {
            get
            {
                return _y;
            }
            set
            {
                _y = value;
                PointChanged(_y);
            }
        }
    
        public event Action<int> PointChanged;
    }




Post a Comment

0 Comments