Class Index
Represent a type can be used to index a collection either from the start or the end.
public class Index : IEquatable<Index>
- Inheritance
-
Index
- Implements
- Inherited Members
Remarks
Index is used by the C# compiler to support the new index syntax
int[] someArray = new int[5] { 1, 2, 3, 4, 5 } ;
int lastElement = someArray[^1]; // lastElement = 5
Constructors
Index(int, bool)
Construct an Index using a value and indicating if the index is from the start or from the end.
public Index(int value, bool fromEnd = false)
Parameters
value
intThe index value. it has to be zero or positive number.
fromEnd
boolIndicating if the index is from the start or from the end.
Remarks
If the Index constructed from the end, index value 1 means pointing at the last element and index value 0 means pointing at beyond last element.
Properties
End
Create an Index pointing at beyond last element.
public static Index End { get; }
Property Value
IsFromEnd
Indicates whether the index is from the start or the end.
public bool IsFromEnd { get; }
Property Value
Start
Create an Index pointing at first element.
public static Index Start { get; }
Property Value
Value
Returns the index value.
public int Value { get; }
Property Value
Methods
Equals(Index)
Indicates whether the current Index object is equal to another Index object.
public bool Equals(Index other)
Parameters
other
IndexAn object to compare with this object
Returns
Equals(object?)
Indicates whether the current Index object is equal to another object of the same type.
public override bool Equals(object? value)
Parameters
value
objectAn object to compare with this object
Returns
FromEnd(int)
Create an Index from the end at the position indicated by the value.
public static Index FromEnd(int value)
Parameters
value
intThe index value from the end.
Returns
FromStart(int)
Create an Index from the start at the position indicated by the value.
public static Index FromStart(int value)
Parameters
value
intThe index value from the start.
Returns
GetHashCode()
Returns the hash code for this instance.
public override int GetHashCode()
Returns
GetOffset(int)
Calculate the offset from the start using the giving collection length.
public int GetOffset(int length)
Parameters
length
intThe length of the collection that the Index will be used with. length has to be a positive value
Returns
Remarks
For performance reason, we don't validate the input length parameter and the returned offset value against negative values. we don't validate either the returned offset is greater than the input length. It is expected Index will be used with collections which always have non negative length/count. If the returned offset is negative and then used to index a collection will get out of range exception which will be same affect as the validation.
ToString()
Converts the value of the current Index object to its equivalent string representation.
public override string ToString()
Returns
Operators
implicit operator Index(int)
Converts integer number to an Index.
public static implicit operator Index(int value)
Parameters
value
int