In This Article

TextRange Struct

Represents a zero-based range of two offset values within a text document, inclusive of the start offset and exclusive of the end offset.

public readonly struct TextRange : IComparable, IComparable<TextRange>, IEquatable<TextRange>
Implements:
IComparable IComparable<TextRange> IEquatable<TextRange>

Constructors

TextRange(int)

Initializes an instance of the structure using the specified offset for both the start and end values.

public TextRange(int offset)
Parameter Type Description
offset int

The start and end offset in the range.

TextRange(int, int)

Represents a zero-based range of two offset values within a text document, inclusive of the start offset and exclusive of the end offset.

public TextRange(int startOffset, int endOffset)
Parameter Type Description
startOffset int

The start offset in the range.

endOffset int

The end offset in the range.

Properties

AbsoluteLength

The absolute number of characters encompassed by the TextRange.

public int AbsoluteLength { get; }

Property Value

int

Remarks

This value will always return a positive length, even if the range is not normalized.

EndOffset

The end offset in the range.

public int EndOffset { get; }

Property Value

int

Remarks

The end offset may occur before the start offset if the range is not normalized. The IsNormalized property indicates if the range is normalized. To get the normalized first offset in the range, use the FirstOffset property. To get the normalized last offset in the range, use the LastOffset property.

FirstOffset

The first offset in the range.

public int FirstOffset { get; }

Property Value

int

Remarks

This property returns either the StartOffset or EndOffset property, whichever comes first.

IsNormalized

Indicates whether the range is normalized (StartOffset occurs at or before the EndOffset).

public bool IsNormalized { get; }

Property Value

bool:

true if the range is normalized; otherwise, false.

IsZeroLength

Indicates whether the range has a zero length.

public bool IsZeroLength { get; }

Property Value

bool:

true if the range has a zero length; otherwise, false.

LastOffset

The last offset in the range.

public int LastOffset { get; }

Property Value

int

Remarks

This property returns either the StartOffset or EndOffset property, whichever comes last.

Length

The number of characters encompassed by the TextRange.

public int Length { get; }

Property Value

int

Remarks

This value will return a negative length if the range is not normalized.

Normalized

A TextRange that ensures the StartOffset occurs at or before the EndOffset offset.

public TextRange Normalized { get; }

Property Value

TextRange

StartOffset

The start offset in the range.

public int StartOffset { get; }

Property Value

int

Remarks

The start offset may occur after the end offset if the range is not normalized. The IsNormalized property indicates if the range is normalized. To get the normalized first offset in the range, use the FirstOffset property. To get the normalized last offset in the range, use the LastOffset property.

Methods

BordersOn(TextRange)

Returns whether the TextRange borders on, but does not overlap the specified TextRange.

public bool BordersOn(TextRange range)
Parameter Type Description
range TextRange

The TextRange to examine.

Returns

bool:

true if the TextRange borders on, but does not overlap the specified TextRange; otherwise, false.

BordersOn(int)

Returns whether the TextRange borders on the specified offset.

public bool BordersOn(int offset)
Parameter Type Description
offset int

The offset to examine.

Returns

bool:

true if the TextRange borders on the specified offset; otherwise, false.

Remarks

This method checks to see if the specified offset is either the StartOffset or EndOffset.

CompareTo(TextRange)

Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.

public int CompareTo(TextRange textRange)
Parameter Type Description
textRange TextRange

Returns

int:

A value that indicates the relative order of the objects being compared. The return value has these meanings:

Value Meaning
Less than zero This instance precedes other in the sort order.
Zero This instance occurs in the same position in the sort order as other.
Greater than zero This instance follows other in the sort order.

Contains(TextRange)

Returns whether the TextRange contains the specified TextRange.

public bool Contains(TextRange range)
Parameter Type Description
range TextRange

The TextRange to examine.

Returns

bool:

true if the TextRange contains the specified TextRange; otherwise, false.

Contains(int)

Returns whether the TextRange contains the specified offset.

public bool Contains(int offset)
Parameter Type Description
offset int

The offset to examine.

Returns

bool:

true if the TextRange contains the specified offset; otherwise, false.

Remarks

This method returns false if the offset falls on the LastOffset offset since this method indicates if the offset is encompassed by the TextRange.

Equals(TextRange)

Indicates whether the current object is equal to another object of the same type.

public bool Equals(TextRange other)
Parameter Type Description
other TextRange

An object to compare with this object.

Returns

bool:

true if the current object is equal to the other parameter; otherwise, false.

Equals(object?)

Indicates whether this instance and a specified object are equal.

public override bool Equals(object? obj)
Parameter Type Description
obj object

The object to compare with the current instance.

Returns

bool:

true if obj and this instance are the same type and represent the same value; otherwise, false.

FromSpan(int, int)

Returns a TextRange that covers the specified offset and length.

public static TextRange FromSpan(int offset, int length)
Parameter Type Description
offset int

The start offset.

length int

The length of the range.

Returns

TextRange

GetHashCode()

Returns the hash code for this instance.

public override int GetHashCode()

Returns

int:

A 32-bit signed integer that is the hash code for this instance.

Intersect(TextRange, TextRange)

Returns the intersection of two TextRange objects.

public static TextRange? Intersect(TextRange range1, TextRange range2)
Parameter Type Description
range1 TextRange

The first TextRange to examine.

range2 TextRange

The second TextRange to examine.

Returns

TextRange?:

The intersection of two TextRange objects.

IntersectsWith(TextRange)

Returns whether the TextRange intersects with the specified TextRange.

public bool IntersectsWith(TextRange range)
Parameter Type Description
range TextRange

The TextRange to examine.

Returns

bool:

true if the TextRange intersects with the specified TextRange; otherwise, false.

Remarks

This method returns whether the specified TextRange borders on or overlaps the TextRange.

IntersectsWith(TextRange, bool, bool)

Returns whether the TextRange intersects with the specified TextRange.

public bool IntersectsWith(TextRange range, bool includeFirstEdge, bool includeLastEdge)
Parameter Type Description
range TextRange

The TextRange to examine.

includeFirstEdge bool

Whether to return true if the specified TextSnapshotRange borders on the leading (first) edge of this TextSnapshotRange.

includeLastEdge bool

Whether to return true if the specified TextSnapshotRange borders on the trailing (last) edge of this TextSnapshotRange.

Returns

bool:

true if the TextRange intersects with the specified TextRange; otherwise, false.

Remarks

This method returns whether the specified TextRange borders on or overlaps the TextRange.

IntersectsWith(int)

Returns whether the TextRange intersects with the specified offset.

public bool IntersectsWith(int offset)
Parameter Type Description
offset int

The offset to examine.

Returns

bool:

true if the TextRange intersects with the specified offset; otherwise, false.

Remarks

This method returns whether the specified offset borders on or lies within the TextRange.

OverlapsWith(TextRange)

Returns whether the TextRange overlaps with, or crosses, the specified TextRange.

public bool OverlapsWith(TextRange range)
Parameter Type Description
range TextRange

The TextRange to examine.

Returns

bool:

true if the TextRange overlaps with, or crosses, the specified TextRange; otherwise, false.

ToString()

Returns the string representation of this object.

public override string ToString()

Returns

string:

The string representation of this object.

Translate(ITextSnapshot, ITextSnapshot, TextRangeTrackingModes)

Translates this text range from one ITextSnapshot to another in the same ITextDocument.

public TextRange? Translate(ITextSnapshot fromSnapshot, ITextSnapshot toSnapshot, TextRangeTrackingModes trackingModes)
Parameter Type Description
fromSnapshot ITextSnapshot

The ITextSnapshot to which to text range belongs.

toSnapshot ITextSnapshot

The ITextSnapshot that is the target of the translation.

trackingModes TextRangeTrackingModes

A TextRangeTrackingModes indicating the tracking modes to use.

Returns

TextRange?:

The translated TextRange, or null if deleted.

Translate(ITextVersion, ITextVersion, TextRangeTrackingModes)

Translates this text range from one ITextVersion to another in the same ITextDocument.

public TextRange? Translate(ITextVersion fromVersion, ITextVersion toVersion, TextRangeTrackingModes trackingModes)
Parameter Type Description
fromVersion ITextVersion

The ITextVersion to which to text range belongs.

toVersion ITextVersion

The ITextVersion that is the target of the translation.

trackingModes TextRangeTrackingModes

A TextRangeTrackingModes indicating the tracking modes to use.

Returns

TextRange?:

The translated TextRange, or null if deleted.

Union(TextRange, TextRange)

Returns the normalized union of two TextRange objects.

public static TextRange Union(TextRange range1, TextRange range2)
Parameter Type Description
range1 TextRange

The first TextRange to examine.

range2 TextRange

The second TextRange to examine.

Returns

TextRange

Operators

operator ==(TextRange, TextRange)

Compares two TextRange objects. The result specifies whether the values of the StartOffset and EndOffset properties of the two TextRange objects are equal.

public static bool operator ==(TextRange left, TextRange right)
Parameter Type Description
left TextRange

A TextRange to compare.

right TextRange

A TextRange to compare.

Returns

bool:

true if the StartOffset and EndOffset values of left and right are equal; otherwise, false.

operator >(TextRange, TextRange)

Compares whether one TextRange is greater than the other.

public static bool operator >(TextRange left, TextRange right)
Parameter Type Description
left TextRange

A TextRange to compare.

right TextRange

A TextRange to compare.

Returns

bool:

true if the TextRange in left is greater than the TextRange in right; otherwise, false.

operator >=(TextRange, TextRange)

Compares whether one TextRange is greater than or equal to the other.

public static bool operator >=(TextRange left, TextRange right)
Parameter Type Description
left TextRange

A TextRange to compare.

right TextRange

A TextRange to compare.

Returns

bool:

true if the TextRange in left is greater than or equal to the TextRange in right; otherwise, false.

operator !=(TextRange, TextRange)

Compares two TextRange objects. The result specifies whether the values of the StartOffset and EndOffset properties of the two TextRange objects are unequal.

public static bool operator !=(TextRange left, TextRange right)
Parameter Type Description
left TextRange

A TextRange to compare.

right TextRange

A TextRange to compare.

Returns

bool:

true if the StartOffset and EndOffset values of left and right are unequal; otherwise, false.

operator <(TextRange, TextRange)

Compares whether one TextRange is less than the other.

public static bool operator <(TextRange left, TextRange right)
Parameter Type Description
left TextRange

A TextRange to compare.

right TextRange

A TextRange to compare.

Returns

bool:

true if the TextRange in left is less than the TextRange in right; otherwise, false.

operator <=(TextRange, TextRange)

Compares whether one TextRange is less than or equal to the other.

public static bool operator <=(TextRange left, TextRange right)
Parameter Type Description
left TextRange

A TextRange to compare.

right TextRange

A TextRange to compare.

Returns

bool:

true if the TextRange in left is less than or equal to the TextRange in right; otherwise, false.

Inherited Members