Skip to content

Instantly share code, notes, and snippets.

Last active Jul 10, 2018
What would you like to do?
Index struct needed by C# 8 range and index features
public readonly struct Index
private readonly int _value;
public int Value => _value < 0 ? ~_value : _value;
public bool FromEnd => _value < 0;
public Index(int value, bool fromEnd)
if (value < 0) throw new ArgumentException("Index must not be negative.", nameof(value));
_value = fromEnd ? ~value : value;
public static implicit operator Index(int value)
=> new Index(value, fromEnd: false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment