Skip to content

Incrementable data types

Arsène von Wyss edited this page Nov 13, 2018 · 1 revision

In order to generically deal with ranges, the library has generic support for dealing with incrementable types. The typical example are the integral .NET types such as int, byte or char.

Incrementable types as used for ranges have the following properties:

  • They are comparable (and in consequence also equatable) to themselves.
  • They can be incremented and decremented in steps of one unit.
  • They have a minimal and a maximal value (usually exposed as MinValue and MaxValue properties or readonly fields).

By leveraging these properties, the generic static Incrementor class provides operations for these:

  • Increment by one
  • Decrement by one
  • Get the maximal value
  • Get the minimal value
  • Test is two values are adjacent

Built-in primitives

Unfortunately, the .NET primitives do not expose a generic interface to perform simple maths on them, which is why the Incrementor class is necessary in the first place for providing the services needed for the generic range algorithms.

The builtin types char, byte, sbyte, short, ushort, int, uint, longand ulong are fully supported.

Custom/native types

Custom types which implement IComparable<> and expose the necessary Increment and Decrement operators as well as MinValue and MaxValue properties should work correctly out of the box.

However, custom types can also implement the IIncrementable<> interface which provides the Increment and Decrement functions without requiring operator overloading. The Codepoint struct for instance provides this support.

Clone this wiki locally