Junior — Middle
What data type is used to store a structure in programming?
sobes.tech AI
Answer from AI
In C#, the struct type is used to define a value type that groups related variables of different types.
Differences between struct and class:
structis a value type, stored on the stack (if not embedded in a reference type), whileclassis a reference type, stored on the heap.structdoes not support inheritance (except implementing interfaces).
Example of a struct in C#:
public struct Point
{
public int X;
public int Y;
public Point(int x, int y)
{
X = x;
Y = y;
}
}
Usage:
Point p = new Point(10, 20);
Console.WriteLine($"X: {p.X}, Y: {p.Y}");
This makes structs suitable for small data objects that do not require inheritance or reference semantics.