C# Structs vs Classes vs Enums Explained

Overview

This code demonstrates three fundamental C# concepts: Structs, Classes, and Enums. The key focus is understanding the difference between value types (structs) and reference types (classes).


1. Enum Example

Code:

public enum Branch
{
    Mechacnical = 77,    // Note: typo in "Mechanical"
    Chemical= 44,
    IT = 4,
    CS=7,
    Civil,               // Will be 8 (CS + 1)
    ENTC                 // Will be 9 (Civil + 1)
}

Usage (when uncommented):

Branch branch = Branch.Civil;
Console.WriteLine(branch);                    // Output: Civil
Console.WriteLine(Convert.ToInt32(branch));   // Output: 8

Key Points:


2. The Critical Difference: Struct vs Class

The Assignment Test

The commented code in the first region demonstrates a crucial concept:

Person person1 = new Person();
person1.No = 10;
person1.Name = "Sachin";

Person person2 = person1;  // This line behaves DIFFERENTLY for struct vs class
person2.Name = "Amit";

Console.WriteLine(person1.Name);  // What will this print?
Console.WriteLine(person2.Name);  // What will this print?