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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.