C# Structure

What is 'Struct' in C#?

Struct in C#

A 'struct' is a lightweight data structure that represents a value type. It is similar to a class, but whereas classes are reference types, structs are value types. This means that when you create an instance of a struct, it is typically stored directly in the memory stack, rather than on the managed heap as with classes. This can lead to performance benefits in certain scenarios, especially for small, frequently used objects.

Key Characteristics of C# Structs:

1. Value Type Semantics: Structs are value types, which means when you assign a struct instance to another variable or pass it to a method, a copy of the struct is made. This contrasts with reference types like classes, where assignment or passing only copies a reference to the object.

2. Default Parameterless Constructor: If you don't define any constructor explicitly, C# provides a default parameterless constructor for structs. This constructor initializes all fields to their default values (e.g., '0' for numeric types, 'null' for reference types).

3. No Inheritance: Unlike classes, structs cannot inherit from other structs or classes, and they cannot be the base of a class. They also cannot implement interfaces directly, although they can indirectly through interfaces implemented by containing classes.

4. No Finalizers: Structs do not support finalizers (~destructors) since they don't rely on garbage collection like classes. Finalizers are used in classes for cleanup tasks before an object is destroyed, but for structs, you don't need to worry about cleanup as they are cleaned up automatically when they go out of scope.

5. Immutable by Convention: It's a common convention to make structs immutable, meaning their state cannot be modified after creation. This is achieved by making fields readonly and providing methods that return new instances with modified values.

When to Use Structs:

6. Small, Lightweight Data Structures: Structs are ideal for representing small, simple data structures that are frequently created and copied, such as geometric points, rectangles, complex numbers, etc.

7. Performance Considerations: If you need to work with many instances of a type and performance is critical, structs can be more efficient than classes due to their value type semantics and stack allocation.

8. Semantic Meaning: Use structs when the semantic meaning of the type is a simple value rather than a complex entity with identity and behavior.

Example of a 'struct' in C# that represents user data:

cs Copy Code
using System;

public struct User
    public int Id;
    public string Username;
    public string Email;

    public User(int id, string username, string email)
        Id = id;
        Username = username;
        Email = email;

    public override readonly string ToString()
        return $"ID: {Id}, Username: {Username}, Email: {Email}";

class Program
    static void Main(string[] args)
        User user1 = new(1, "ayan_sarkar", "ayan@example.com");
        User user2 = new(2, "apu_malik", "apu@example.com");

ID: 1, Username: ayan_sarkar, Email: ayan@example.com
ID: 2, Username: apu_malik, Email: apu@example.com

This demonstrates how you can use a struct to store user data in a simple and efficient manner.

What's Next?

We've now entered the finance section on this platform, where you can enhance your financial literacy.