What is the Structure and why we need it although we have a class?
In C#, a structure is a value type that can contain fields, properties, methods, and events. It is similar to a class in terms of defining members, but it differs in some fundamental ways. Here's an overview of structures and why we use them:
-
Value Type: Unlike classes, which are reference types, structures are value types. When you create an instance of a structure, it is stored on the stack or as part of another object. Value types are copied by value when assigned to variables or passed as method arguments. This can be beneficial in certain scenarios where you want lightweight and efficient data storage and pass-by-value semantics.
-
Performance and Memory Efficiency: Structures are typically used for small, lightweight objects that don't require complex behavior or inheritance. Since structures are value types and are stored directly in memory, they can be more memory-efficient and have better performance compared to classes.
-
Immutable by Default: By default, structures in C# are immutable, meaning their state cannot be changed after they are created. This can help ensure thread safety and eliminate unintended modification of data.
-
Stack Allocation: Structures are often allocated on the stack, which makes their creation and destruction faster compared to objects allocated on the heap. This can be advantageous when dealing with large numbers of small objects or when performance is critical.
-
Semantic Differences: Structures have different behavior than classes in certain scenarios. For example, when you assign a structure to another variable, a copy of the structure's value is created. Modifying one variable doesn't affect the other. In contrast, with classes, assigning one variable to another creates a reference to the same object.
-
Use Cases: Structures are commonly used for simple data containers, representing mathematical concepts, coordinates, date/time values, and other small entities that don't require complex behavior or relationships. They are also widely used in the .NET Framework for types such as DateTime, Point, and Color.
While classes are more flexible and suitable for modeling complex entities, structures provide a lightweight alternative for representing small, immutable data objects with value semantics. The choice between classes and structures depends on the specific requirements of your application, performance considerations, and the nature of the data you are working with.
In order to create a structure, you must use the 'struct' statement. The 'struct' statement allows you to define a new variable with multiple members of your structure. Let's illustrate this with an example of declaring and initializing a structure:
struct StudentRecord
{
public string Id;
public string FirstName;
public string LastName;
public int ClassGrade;
public Address Adrs;
}
Next, we will proceed to set the values for each field within the structure:
void addStudent()
{
StudentRecord std1;
std1.Id = "1";
std1.FirstName = "john";
std1.LastName = "smith";
std1.ClassGrade = 10;
Console.WriteLine($"Id: {std1.Id}");
Console.WriteLine($"Firt Name: {std1.FirstName}");
Console.WriteLine($"Last Name: {std1.LastName}");
Console.WriteLine($"Class Grade: {std1.ClassGrade}");
}
In the example provided above, a structure named 'std1' is created, and we can access all its members using the dot (.) operator.
Output:
Id: 1
First Name: john
Last Name: smith
Class Grade: 10