Sobes.tech
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:

  • struct is a value type, stored on the stack (if not embedded in a reference type), while class is a reference type, stored on the heap.
  • struct does 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.