Skip to content

Instantly share code, notes, and snippets.

@christiannagel
Last active July 10, 2018 14:50
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save christiannagel/3dc61b8e40659ba86e0e0dd9d86301f3 to your computer and use it in GitHub Desktop.
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