C# -

Structs

Structs in C# are value types that are typically used to encapsulate small groups of related variables. Unlike classes, structs are allocated on the stack and can provide better performance for certain use cases. This tutorial covers the basics of structs, their differences from classes, when to use structs, and best practices.


1. Understanding Structs

Structs are value types, which means they are stored on the stack and their values are directly accessible. This differs from reference types, such as classes, which are stored on the heap and accessed via references. Structs are useful for representing lightweight objects.

Example:
        
            namespace StructExamples;

// Define a struct
struct Point
{
    public int X { get; set; }
    public int Y { get; set; }

    public Point(int x, int y)
    {
        X = x;
        Y = y;
    }

    public void Display()
    {
        Console.WriteLine($"Point: ({X}, {Y})");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Point point = new Point(3, 4);
        point.Display(); // Output: Point: (3, 4)
    }
}
        
    

2. Differences Between Structs and Classes

While structs and classes are both used to define objects, they have several key differences:

Example:
        
            namespace StructExamples;

// Define a struct
struct Point
{
    public int X { get; set; }
    public int Y { get; set; }

    public Point(int x, int y)
    {
        X = x;
        Y = y;
    }
}

// Define a class
class PointClass
{
    public int X { get; set; }
    public int Y { get; set; }

    public PointClass(int x, int y)
    {
        X = x;
        Y = y;
    }
}

class Program
{
    static void Main(string[] args)
    {
        Point pointStruct = new Point(3, 4);
        PointClass pointClass = new PointClass(3, 4);

        // Value type: copied by value
        Point anotherPointStruct = pointStruct;
        anotherPointStruct.X = 5;

        // Reference type: copied by reference
        PointClass anotherPointClass = pointClass;
        anotherPointClass.X = 5;

        Console.WriteLine($"pointStruct.X: {pointStruct.X}, anotherPointStruct.X: {anotherPointStruct.X}"); // Output: pointStruct.X: 3, anotherPointStruct.X: 5
        Console.WriteLine($"pointClass.X: {pointClass.X}, anotherPointClass.X: {anotherPointClass.X}"); // Output: pointClass.X: 5, anotherPointClass.X: 5
    }
}
        
    

3. When to Use Structs

Structs should be used when you need a lightweight object that represents a single value or a small group of related values. Common scenarios include:


4. Struct Members

Structs can contain fields, properties, methods, and constructors with parameters. However, structs cannot contain default constructors, destructors, or finalizers.

Example:
        
            namespace StructExamples;

// Define a struct with members
struct Rectangle
{
    public int Width { get; }
    public int Height { get; }

    public Rectangle(int width, int height)
    {
        Width = width;
        Height = height;
    }

    public int Area()
    {
        return Width * Height;
    }

    public override string ToString()
    {
        return $"Rectangle: {Width}x{Height}";
    }
}

class Program
{
    static void Main(string[] args)
    {
        Rectangle rect = new Rectangle(3, 4);
        Console.WriteLine(rect); // Output: Rectangle: 3x4
        Console.WriteLine($"Area: {rect.Area()}"); // Output: Area: 12
    }
}
        
    

5. Best Practices for Using Structs



Conclusion

Structs in C# are value types that are useful for encapsulating small, related groups of variables. By understanding their differences from classes, when to use them, and best practices, you can effectively leverage structs to build performant and maintainable applications.