Struct

Struct (structure) is like a class in C# which combines the fields and methods into a single unit. The function of struct is exactly similar to class with some differences. We'll discuss each and every difference below.

Class Struct
class is a reference type Struct is a value type
class can have default constructor struct cannot have default constructor
Class can inherit from another class struct cannot inherit from another struct or class
Abstraction is possible in class Abstraction is not possible in struct
All the reference types are allocated on heap memory All the value types are allocated on stack memory

CODE

EG 1 . Creating a struct and a class. Creating 2 instance of both and assigning to each other.

class rectangleClass {
    public double length, width;
    public rectangleClass(double l,double w) {
        length = l;
        width = w;
    }
    public double Area() {
        return length * width;
    }
}

struct rectangleStruct {
    public double length, width;
    public rectangleStruct(double l, double w) {
        length = l;
        width = w;
    }
    public double Area() {
        return length * width;
    }
}

class Program {
    static void main() {
        //-------- STRUCT --------
        rectangleStruct r1 = new rectangleStruct(10,12);
        Console.WriteLine("R1 Length = "+r1.length);//OUTPUT : 10
        Console.WriteLine("R1 Width = "+r1.width);//OUTPUT : 12
        Console.WriteLine("R1 Area = "+r1.Area());//OUTPUT : 120

        rectangleStruct r2 = r1;
        Console.WriteLine("R1 Length = " + r1.length);///OUTPUT : 10
        Console.WriteLine("R1 Width = " + r1.width);///OUTPUT : 15
        Console.WriteLine("R1 Area = " + r1.Area());///OUTPUT : 150

        Console.WriteLine("R2 Length = " + r2.length);///OUTPUT : 10
        Console.WriteLine("R2 Width = " + r2.width);///OUTPUT : 15
        Console.WriteLine("R2 Area = " + r2.Area());///OUTPUT : 150

        r1.width = 20;
        Console.WriteLine("R1 Length = " + r1.length);//OUTPUT : 10
        Console.WriteLine("R1 Width = " + r1.width);//OUTPUT : 20
        Console.WriteLine("R1 Area = " + r1.Area());//OUTPUT : 200

        Console.WriteLine("R2 Length = " + r2.length);//OUTPUT : 10
        Console.WriteLine("R2 Width = " + r2.width);//OUTPUT : 15
        Console.WriteLine("R2 Area = " + r2.Area());//OUTPUT : 150

        //-------- CLASS --------
        rectangleClass r1 = new rectangleClass(10,12);
        Console.WriteLine("R1 Length = "+r1.length);//OUTPUT : 10
        Console.WriteLine("R1 Width = "+r1.width);//OUTPUT : 12
        Console.WriteLine("R1 Area = "+r1.Area());//OUTPUT : 120

        rectangleClass r2 = r1;
        Console.WriteLine("R1 Length = " + r1.length);///OUTPUT : 10
        Console.WriteLine("R1 Width = " + r1.width);///OUTPUT : 15
        Console.WriteLine("R1 Area = " + r1.Area());///OUTPUT : 150

        Console.WriteLine("R2 Length = " + r2.length);///OUTPUT : 10
        Console.WriteLine("R2 Width = " + r2.width);///OUTPUT : 15
        Console.WriteLine("R2 Area = " + r2.Area());///OUTPUT : 150

        r1.width = 20;
        Console.WriteLine("R1 Length = " + r1.length);//OUTPUT : 10
        Console.WriteLine("R1 Width = " + r1.width);//OUTPUT : 20
        Console.WriteLine("R1 Area = " + r1.Area());//OUTPUT : 200

        Console.WriteLine("R2 Length = " + r2.length);//OUTPUT : 10
        Console.WriteLine("R2 Width = " + r2.width); //OUTPUT : 20
        Console.WriteLine("R2 Area = " + r2.Area());//OUTPUT : 200
    }
}