Understanding struct
in C#
A struct
(short for structure) in C# is a value type used to represent a collection of related data fields where each field can be of a different type. Unlike an array, where all elements must be of the same type, a struct
allows you to group different data types together in a single unit. This concept is similar to a record in other programming languages.
Characteristics of struct
- Value Type: Unlike classes, which are reference types, structs are value types. This means they are stored on the stack, not the heap, and when you assign a struct to another struct, a copy of the data is made.
- No Inheritance: Structs cannot inherit from other structs or classes, but they can implement interfaces.
- Lightweight: Structs are generally used for small, simple data structures that do not require the overhead of a class.
Structs are ideal for representing small data structures that have a fixed format, such as the ones we’ll use in our examples. Getting familiar with structs will also help you understand the basics of classes, as they share many similar features.
Defining a struct
For our simple TODO application, a struct
could be defined as follows:
This struct
has three fields:
TaskDueDate
: ADateTime
field to store the due date of the task.Task
: Astring
field for the task description.Status
: Astring
field for the status of the task (e.g., "In Progress", "Not Started").
Declaring and Using a struct
We can declare a variable of the Todo
struct type as follows:
To use the struct in our program, we access each field using the dot (.
) operator:
Using the new
Keyword
When you create a struct without using the new
keyword, all fields must be assigned a value before you can use them:
Alternatively, you can use the new
keyword to initialize the struct:
Adding Methods to a struct
A struct can contain methods to operate on its data. For example, we can add a method to convert the struct data to a CSV format:
This method returns a comma-separated string of the struct’s fields, making it easy to write to a file:
Using ToString()
Method
The ToCSV()
method is convenient for specific formats like CSV. However, in C#, all structs and classes inherit a ToString()
method, which can be overridden to provide a string representation of the struct:
Using this method, we can easily display the struct data:
Initializing and Using an Array of Structs
You can also create an array of structs to store multiple tasks:
Alternatively, you can use a list for more flexibility:
Constructors and Properties
Structs can also have constructors and properties, similar to classes. A constructor is used to initialize the struct with specific values:
Now you can create and initialize a struct in a single statement:
Summary
- Value Type: Structs are stored on the stack and are copied when assigned to another variable.
- Lightweight: Suitable for small, immutable data structures.
- No Inheritance: Structs cannot inherit from other structs or classes, but they can implement interfaces.
- Methods and Properties: Structs can have their own methods, properties, and constructors, making them versatile.
Exercises
- Create a Struct for Students: Define a
Student
struct with fields for name, age, and grade. Create an array of students and display their details. - Implement a
ToString()
Method: For theStudent
struct, override theToString()
method to provide a custom string representation. - Manage a List of Todos: Expand the TODO application to handle a list of
Todo
structs, allowing users to add, edit, and remove tasks.