81. Complex numbers

The ComplexNumber structure is fairly long, so I'll describe it in pieces.

You could make ComplexNumber either a class or a structure. Many programmers use classes exclusively. This isn't a terrible choice, in part because it frees you from having to deal with the differences between classes and structures.

I’ve decided to make ComplexNumber a structure for a couple of reasons. Microsoft's guidelines generally recommend using a structure if the item meets the following criteria:

  • It is relatively small
  • It logically represents a primitive type such as a number
  • It is immutable, so its value does not change after it has been created
  • It will not need to be boxed and unboxed often

Get The Modern C# Challenge 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.