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:
System.ICloneable System.IComparable ITextRangeProvider

Constructors

TextRange(Int32)

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

The start and end offset in the range.

TextRange(Int32, Int32)

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

public TextRange(int startOffset, int endOffset)
Parameter Type Description
startOffset System.Int32

The start offset in the range.

endOffset System.Int32

The end offset in the range.

Properties

AbsoluteLength

Gets the absolute number of characters encompassed by the TextRange.

public int AbsoluteLength { get; }

Property Value

System.Int32:

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

System.Int32:

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

System.Int32:

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

System.Boolean:

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

System.Boolean:

true if the range is normalized; otherwise, false.

IsZeroLength

Gets whether the range has a zero length.

public bool IsZeroLength { get; }

Property Value

System.Boolean:

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

LastOffset

Gets the last offset in the range.

public int LastOffset { get; }

Property Value

System.Int32:

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

System.Int32:

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

System.Int32:

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

System.Boolean:

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

BordersOn(Int32)

Returns whether the TextRange borders on the specified offset.

public bool BordersOn(int offset)
Parameter Type Description
offset System.Int32

The offset to examine.

Returns

System.Boolean:

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

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

System.Int32:

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

System.Boolean:

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

Contains(Int32)

Returns whether the TextRange contains the specified offset.

public bool Contains(int offset)
Parameter Type Description
offset System.Int32

The offset to examine.

Returns

System.Boolean:

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

The Object to compare to the current Object.

Returns

System.Boolean:

true if the specified Object is equal to the current Object; otherwise, false.

FromSpan(Int32, Int32)

Returns a TextRange that covers the specified offset and length.

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

The start offset.

length System.Int32

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

System.Int32:

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

System.Boolean:

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, Boolean, Boolean)

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

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

includeLastEdge System.Boolean

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

Returns

System.Boolean:

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

Returns whether the TextRange intersects with the specified offset.

public bool IntersectsWith(int offset)
Parameter Type Description
offset System.Int32

The offset to examine.

Returns

System.Boolean:

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

System.Boolean:

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

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

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

System.Boolean:

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

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

System.Boolean:

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

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

System.Boolean:

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

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

System.Boolean:

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

Explicit Interface Implementations

ITextRangeProvider.TextRange

Gets or sets a TextRange that specifies the text range of the object.

TextRange ITextRangeProvider.TextRange { get; set; }

Returns

TextRange:

A TextRange that specifies the text range of the object.

IComparable.CompareTo(Object)

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

int IComparable.CompareTo(object obj)
Parameter Type Description
obj System.Object

An object to compare with this instance.

Returns

System.Int32:

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

Inherited Members

  • System.Object.Equals(System.Object, System.Object)
  • System.Object.ReferenceEquals(System.Object, System.Object)
  • System.Object.GetType()