C# Structs & Enum

HTML
CSS
C#
SQL

Understanding Structs in C#

Value Types:

structs are value types that represent lightweight objects.

They are suitable for small, simple data structures that don’t require complex behavior.

public struct Point

{

    // Struct fields

    public int X;

    public int Y;

}

Initialization:

Unlike classes, structs can be initialized without using the new keyword.

// Initializing a struct

Point point = new Point();

point.X = 5;

point.Y = 10;

// Initializing a struct without new keyword

Point point1;

point1.X = 20;

point1.Y = 30;

Using Structs in Practice

Structs are passed by value, meaning a copy of the struct is passed to methods.

public struct Dimensions

{

    public int Width;

    public int Height;

}

public class ShapeCalculator

{

    public static int CalculateArea(Dimensions dimensions)

    {

        return dimensions.Width * dimensions.Height;

    }

}

Example usage

Dimensions rectangleSize = new Dimensions { Width = 5, Height = 10 };

int area = ShapeCalculator.CalculateArea(rectangleSize);

Mutability:

Structs are value types, and each instance is independent of others.

Changes to one instance do not affect other instances.

Dimensions rect1 = new Dimensions { Width = 5, Height = 10 };

Dimensions rect2 = rect1; // Copying values

rect1.Width = 8;

Console.WriteLine(rect1.Width); // Outputs 8

Console.WriteLine(rect2.Width); // Outputs 5

Understanding Enums in C#

Named Integral Values:

enums allow you to define named integral values, providing clarity and readability to your code.

public enum DaysOfWeek

{

    Sunday,

    Monday,

    Tuesday,

    Wednesday,

    Thursday,

    Friday,

    Saturday

}

Underlying Type:

Enums have an underlying type, usually int by default, but you can specify other integral types.

public enum TrafficLight: short

{

    Red = 1,

    Yellow = 2,

    Green = 3

}

Using Enums in Practice

Enums are often used in switch statements to handle specific cases.

public enum TrafficLight

{

    Red,

    Yellow,

    Green

}

 

public class TrafficLightController

{

    public void HandleTrafficLight(TrafficLight light)

    {

        switch (light)

        {

            case TrafficLight.Red:

               Console.WriteLine(“Stop!”);

                break;

            case TrafficLight.Yellow:

                Console.WriteLine(“Prepare to stop!”);

                break;

            case TrafficLight.Green:

                Console.WriteLine(“Go!”);

                break;

            default:

                Console.WriteLine(“Unknown state”);

                break;

        }

    }

}

Example usage

TrafficLightController controller = new TrafficLightController();

controller.HandleTrafficLight(TrafficLight.Green);

Underlying Values:

You can access the underlying values of enums.

int greenValue = (int)TrafficLight.Green; // greenValue is 2

Flags Attribute:

Enums can be marked with the Flags attribute to enable bitwise operations.

[Flags]

public enum Permissions

{

    Read = 1,

    Write = 2,

    Execute = 4

}

Example usage of bitwise operations

Permissions userPermissions = Permissions.Read | Permissions.Write;

Practices Tasks

1. Define the Rectangle Struct. This struct will contain two private variables, Width and Height, which are accessible only within the struct. Create public properties named Width and Height. Include a method named CalculateArea() within the struct. This method calculates and returns the area of the rectangle by multiplying its width and height. Inside the Main() method, create an instance of the Rectangle struct and pass parameters as 5.0 and 3.0. Then call the CalculateArea() method using the instance

OUTPUT 

2. Define an enum named “DayOfWeek” representing the days of the week. In the Main() method, assign the value “Friday” to a variable named “today” of type “DayOfWeek”. Then, print out the value of “today” to the console.

OUTPUT