26.3. Why use a struct?

Even if you have never heard of structs before reading this chapter, the chances are that you have already used them quite a bit. Simple (or primitive) types in C# are implemented as structs internally. [14]

[14] Your sbyte, short, int, long, byte, ushort, uint, ulong, float, double, decimal, char, and bool types are all internally implemented as structs.

You should choose to use a struct rather than a class for performance reasons. If you are writing a class that:

  • represents a small data structure with few data members, [15] and

    [15] Examples of simple data structures better implemented as structs than classes include a complex number (with only two fields – a real part and an imaginary part), a point of the coordinate ...

Get From Java to C#: A Developer's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.