In This Article

TextRange Struct

Represents a zero-based range of two offset values within a text document.

public struct TextRange : ICloneable, IComparable, ITextRangeProvider
Implements:
ICloneable IComparable ITextRangeProvider

Constructors

TextRange(int)

Initializes a new instance of the TextRange class 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)

Initializes a new instance of the TextRange class with the specified offset values.

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

Gets the absolute number of characters encompassed by the TextRange.

public int AbsoluteLength { get; }

Property Value

int:

The absolute number of characters encompassed by the TextRange.

Remarks

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

Deleted

Gets a TextRange that is flagged as deleted.

public static TextRange Deleted { get; }

Property Value

TextRange:

A TextRange that is flagged as deleted.

EndOffset

Gets the end offset in the range.

public int EndOffset { get; }

Property Value

int:

The end offset in the range.

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

Gets the first offset in the range.

public int FirstOffset { get; }

Property Value

int:

The first offset in the range.

Remarks

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

IsDeleted

Gets whether the range has been flagged as deleted.

public bool IsDeleted { get; }

Property Value

bool:

true if the range has been flagged as deleted; otherwise, false.

IsNormalized

Gets 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

Gets 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

Gets the last offset in the range.

public int LastOffset { get; }

Property Value

int:

The last offset in the range.

Remarks

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

Length

Gets the number of characters encompassed by the TextRange.

public int Length { get; }

Property Value

int:

The number of characters encompassed by the TextRange.

Remarks

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

NormalizedTextRange

Gets a normalized version of this TextRange without actually modifying this instance.

public TextRange NormalizedTextRange { get; }

Property Value

TextRange:

A normalized version of this TextRange without actually modifying this instance.

StartOffset

Gets the start offset in the range.

public int StartOffset { get; }

Property Value

int:

The start offset in the range.

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.

Clone()

Creates a copy of this TextRange object.

public object Clone()

Returns

object:

The TextRange object that this method creates.

CompareTo(TextRange)

Compares the current instance with another object of the same type.

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

A TextRange to compare with this instance.

Returns

int:

A 32-bit signed integer that indicates the relative order of the comparands.

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(object)

Determines whether the specified Object is equal to the current Object.

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

The Object to compare to the current Object.

Returns

bool:

true if the specified Object is equal to the current Object; 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:

The TextRange that was created.

GetHashCode()

Returns a hash code for this object.

public override int GetHashCode()

Returns

int:

An integer value that specifies a hash value for this object.

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.

Invert()

Inverts the range by flipping the StartOffset and EndOffset values;

public void Invert()

Normalize()

Normalizes the range and ensures that the StartOffset occurs at or before the EndOffset offset.

public void Normalize()

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()

Converts the object to a String.

public override string ToString()

Returns

string:

A string whose value represents 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.

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.

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:

The normalized union of two TextRange objects.

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 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.

Inherited Members