-
-
Save christiannagel/3dc61b8e40659ba86e0e0dd9d86301f3 to your computer and use it in GitHub Desktop.
Index struct needed by C# 8 range and index features
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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