Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

Understanding C# Structs: Definition, Usage, and Key Differences

C# Structs

Learn how to define, instantiate, and work with C# structs, including constructors, properties, and their key differences from classes.

In C#, a struct (structure) functions similarly to a class but is a value type, meaning it stores data directly rather than a reference.

Suppose we want to store the name and age of a person. We can create two variables: name and age and store values.

However, if we need to hold this information for multiple people, declaring separate variables becomes tedious. Defining a struct that contains name and age lets us reuse the same layout for every person.


Define struct in C#

In C#, we use the struct keyword to declare a struct. For example:

struct Employee  {
  public int id;
}

Here, id is a field inside the struct. A struct can also contain methods, indexers, and more.


Declare struct variable

Before using a struct, you must create a struct variable. The syntax is:

struct Employee {
  public int id;
}
// declare emp of struct Employee
Employee emp;

In the example, we defined a struct named Employee and declared a variable emp of that type.


Access C# struct

Access struct members using the dot (.) operator:

Employee emp;
emp.id = 1;

This assigns the value 1 to the id field of the Employee struct.

Note: Primitive types such as int, bool, and float are pre‑defined structs in C#.


Example: C# Struct

using System;
namespace CsharpStruct {
  // defining struct
  struct Employee {
    public int id;

    public void getId(int id) {
      Console.WriteLine("Employee Id: " + id);
    }
  }

  class Program {
    static void Main(string[] args) {
      // declare emp of struct Employee
      Employee emp;

      // accesses and sets struct field
      emp.id = 1;

      // accesses struct methods
      emp.getId(emp.id);

      Console.ReadLine();
    }
  }
}

Output

Employee Id: 1

The program creates an Employee struct, sets its id field, and calls the getId method to display it.

You can also instantiate a struct with the new keyword:

Employee emp = new Employee();

This calls the parameterless constructor and initializes all fields to their default values.


Constructors in C# struct

Structs may define constructors to initialize fields. Example:

struct Employee {
  public int id;

  // constructor 
  public Employee(int employeeId) {
    id = employeeId;
  }
}

Note: Prior to C# 10, structs cannot contain parameterless constructors.


Example: Constructor in C# structs

using System;
namespace CsharpStruct {
  // defining struct
  struct Employee {
    public int id;
    public string name;

    // parameterized constructor
    public Employee(int employeeId, string employeeName) {
      id = employeeId;
      name = employeeName;
    }
  }

  class Program {
    static void Main(string[] args) {
      // calls constructor of struct
      Employee emp = new Employee(1, "Brian");
      Console.WriteLine("Employee Name: " + emp.name);
      Console.WriteLine("Employee Id: " + emp.id);
      Console.ReadLine();
    }
  }
}

Output

Employee Name: Brian
Employee Id: 1

In this example, the constructor assigns values to all fields. Failing to initialize every field results in a compile‑time error.


Properties in C# struct

Structs can expose properties for encapsulation. Example:

using System;
namespace CsharpStruct {
  // defining struct
  struct Employee {
    public int id;

    // creates property
    public int Id {
      get { return id; }
      set { id = value; }
    }
  }

  class Program {
    static void Main(string[] args) {
      Employee emp = new Employee();
      emp.Id = 1;
      Console.WriteLine("Employee Id: " + emp.Id);
      Console.ReadLine();
    }
  }
}

Output

Employee Id: 1

Here, the Id property provides controlled access to the id field.


Difference between class and struct in C#

Although classes and structs share similar syntax, they differ fundamentally:

Example of class reference behavior:

using System;
namespace CsharpStruct {
  class Employee {
    public string name;
  }

  class Program {
    static void Main(string[] args) {
      Employee emp1 = new Employee();
      emp1.name = "John";
      Employee emp2 = emp1;
      emp2.name = "Ed";
      Console.WriteLine("Employee1 name: " + emp1.name);
      Console.ReadLine();
    }
  }
}

Output

Employee1 name: Ed

Example of struct value behavior:

using System;
namespace CsharpStruct {
  struct Employee {
    public string name;
  }

  class Program {
    static void Main(string[] args) {
      Employee emp1 = new Employee();
      emp1.name = "John";
      Employee emp2 = emp1;
      emp2.name = "Ed";
      Console.WriteLine("Employee1 name: " + emp1.name);
      Console.ReadLine();
    }
  }
}

Output

Employee1 name: John

In the struct example, emp2 receives a copy of emp1, so modifications to emp2 do not affect emp1.

These distinctions make structs ideal for small, immutable data structures that benefit from stack allocation and value semantics.

C Language

  1. Bill Ryan Named February 2021 Employee of the Month
  2. March 2021 Employee of the Month: Robin Nichols – A Dedicated HR Professional
  3. Monroe’s April 2021 Employee of the Month: Brittney Rodgers
  4. June 2021 Employee of the Month: Sam Hecht, General Manager – Warsaw
  5. Employee of the Month – Gary McClure: A Decade of Sales Excellence
  6. November 2021 Employee of the Month: Tracy Walker – Excellence in Service & Leadership
  7. Mastering C Structs: Definition, Variables, Access & Advanced Examples
  8. Mastering C Structs and Pointers: A Practical Guide
  9. Mastering C Structs: Passing, Returning, and Reference Handling
  10. C++ Structs Explained with a Practical Example