O'Reilly logo

From Java to C#: A Developer's Guide by Heng Ngee Mok

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required