Similar to a class in C#, a struct is also a data structure that consists of members, functions, and so on. Classes are reference types, but structs are value types; hence, these are not required for heap allocation but for allocation on the stack.
Value type data will be allocated on stack and reference type data will be allocated on heap. A value type that is used in struct is stored on the stack, but when the same value type is used in an array, it is stored in a heap.
Note
For more details on heap and stack memory allocation, refer to http://www-ee.eng.hawaii.edu/~tep/EE160/Book/chap14/subsection2.1.1.8.html and https://www.codeproject.com/Articles/1058126/Memory-allocation-in-Net-Value-type-Reference-type.
So, when you create a variable of struct
type, that variable directly stores data instead of reference, as is the case with classes. In C#, the struct
keyword (refer to section C# keywords for more detail) helps declare structures. Structures are helpful in representing...