A struct a value type and it store it's data in stack. Classes are reference type and it store it's data in heap. The structs are usually used for small data structures. The structs are specially used when we don't need features of classes like inheritance.
Suppose we want to store the name and color of a fruit. We can create two variables: name and color and store value.
However, suppose we want to store the same information of multiple fruit.
In this case, creating variables for an individual fruit might be a tedious task. To overcome this we can create a struct that stores name and color. Now, this struct can be used for every fruit.
In C#, we use the struct keyword to define a struct. For example,
Here, in below example of struct field
struct - struct is keword that is used to define a struct.
Employee - Employee is a name of struct.
fruitPrice - fruitPrice is a field inside struct.
In C#, we use the struct keyword to define a struct with named Fruit and then decalre it with Fruit frut; object. For example,
Here, in below example of struct field
struct - struct is keword that is used to define a struct.
Employee - Employee is a name of struct.
fruitPrice - fruitPrice is a field inside struct.
Here, in below example of declaring a struct
Fruit frut; - In this above code, we have created a struct named Employee. Here, we have declared a variable frut of the struct Fruit.
// declare fruit of struct Employee
// declare fruit of struct Employee
We use the struct variable along with the . operator to access members of a struct. For example,
Here, in below example of struct field
struct - struct is keword that is used to define a struct.
Employee - Employee is a name of struct.
fruitPrice - fruitPrice is a field inside struct.
Here, in below example of declaring a struct
Fruit frut; - Here, we have used variable frut of a struct Fruit with . operator to access members of the Fruit.
Here, in below example of accessing member of structt
frut.fruitPrice = 5; - This accesses the fruitPriceid field of struct Fruit.
// declare fruit of struct Employee
// access member of struct
// declare fruit of struct Employee
// access member of struct
In C#, a struct can also include constructors. For example,
Here, in below example of struct field
struct - struct is keword that is used to define a struct.
Employee - Employee is a name of struct.
fruitPrice - fruitPrice is a field inside struct.
Here, in below example of declaring a struct
Fruit frut; - Here, we have used variable frut of a struct Fruit with . operator to access members of the Fruit.
Here, in below example of accessing member of structt
frut.fruitPrice = 5; - This accesses the fruitPriceid field of struct Fruit.
// constructor
// calls constructor of struct
// constructor
// calls constructor of struct
Here, we have created a parameterized constructor Fruit() with parameter price, name and isavailable respectively. We are using the new keyword to call the constructor. Here, 10 , "Mango" and true are arguments passed to the constructor, where they are assigned to the parameters FruitPrice, FruitName and FruitIsInMarket respectively."
In below code, Fruit is struct which have fields FruitPrice, FruitPrice and FruitIsInMarket respectively. We also have parameterized constructor Fruit() for initializing the value to struct member. We have AddFruit method for making sum of two prices of fruit.
We have created frut1 object of Fruit Struct with value, 10, Mango, true respectively. We are assinging a copy of Fruit struct fruit1 to fruit2. We have modified the value of FruitPrice inside frut2 object.
// Creates a copy of p1
// Modifying p2 does not affect p1
// Output: frut1: (10, 20)
// Output: frut2: (30, 20
// Struct Fields
// Struct Constructor
// Initialization code for struct members
// Struct Method
// Creates a copy of p1
// Modifying p2 does not affect p1
// Output: frut1: (10, 20)
// Output: frut2: (30, 20
// Struct Fields
// Struct Constructor
// Initialization code for struct members
// Struct Method
When you create a variable of struct type, the data of this variable is directly stored in the memory. When you assign a struct variable to another another, the whole data is copied directly into another variable not reference is assigned to it.
The struct are allocated on the stack. We have more better efficiency of memory management. But on there other hand struct are not efficient for the large amount of data.
Unlike classes, structs cannot inherit from other types, and they cannot be used as a base for other types. They can implement interfaces, though.
It's a good practice to make structs immutable (i.e., their fields cannot be changed after initialization) to ensure predictability and avoid unexpected behavior.
C# generates a default constructor that initializes all fields to their default values. If you don't provide a constructor for a struct.
When you pass a struct as an argument to a method or assign it to another variable, a copy of the struct is made. This copy behavior can lead to unexpected results if you're not aware of it.
In summary, structs in c# are used for creating lightweight, stack-allocated data types with value semantics. They are suitable for small data structures where copying the entire value is acceptable and provide better performance and memory efficiency in certain scenarios compared to classes.