Class Range<T>
- Namespace
- Ecng.ComponentModel
- Assembly
- Ecng.ComponentModel.dll
Represents a generic range defined by a minimum and maximum value.
public class Range<T> : Equatable<Range<T>>, ICloneable<Range<T>>, ICloneable, IEquatable<Range<T>>, IComparable<Range<T>>, IComparable, IConvertible, IRange where T : IComparable<T>
Type Parameters
T
The type of the range values. Must implement IComparable<T>.
- Inheritance
-
Range<T>
- Implements
-
ICloneable<Range<T>>IEquatable<Range<T>>IComparable<Range<T>>
- Inherited Members
- Extension Methods
Constructors
Range()
Initializes a new instance of the Range<T> class.
public Range()
Range(T, T)
Initializes a new instance of the Range<T> class with specified minimum and maximum values.
public Range(T min, T max)
Parameters
min
TThe minimum value of the range.
max
TThe maximum value of the range.
Fields
MaxValue
Represents the largest possible value of type T
.
public static readonly T MaxValue
Field Value
- T
MinValue
Represents the smallest possible value of type T
.
public static readonly T MinValue
Field Value
- T
Properties
HasMaxValue
Gets a value indicating whether the range has a specified maximum value.
[Browsable(false)]
public bool HasMaxValue { get; }
Property Value
HasMinValue
Gets a value indicating whether the range has a specified minimum value.
[Browsable(false)]
public bool HasMinValue { get; }
Property Value
Length
Gets the difference between the maximum and minimum values of the range. Returns MaxValue if either bound is not defined.
[Browsable(false)]
public T Length { get; }
Property Value
- T
Max
Gets or sets the maximum value of the range.
public T Max { get; set; }
Property Value
- T
Exceptions
- ArgumentOutOfRangeException
Thrown when the new maximum value is less than the current minimum value.
Min
Gets or sets the minimum value of the range.
public T Min { get; set; }
Property Value
- T
Exceptions
- ArgumentOutOfRangeException
Thrown when the new minimum value is greater than the current maximum value.
Methods
Clone()
Creates a new object that is a copy of the current instance.
public override Range<T> Clone()
Returns
- Range<T>
A new object that is a copy of this instance.
Contains(Range<T>)
Determines whether the current range completely contains another range.
public bool Contains(Range<T> range)
Parameters
range
Range<T>The range to check against.
Returns
- bool
true if the range is contained; otherwise, false.
Exceptions
- ArgumentNullException
Thrown if the provided range is null.
Contains(T)
Determines whether the specified value is within the current range.
public bool Contains(T value)
Parameters
value
TThe value to test.
Returns
- bool
true if the value is within the range; otherwise, false.
GetHashCode()
Serves as the default hash function.
public override int GetHashCode()
Returns
- int
A hash code for the current object.
Intersect(Range<T>)
Returns the intersection of the current range with another range.
public Range<T> Intersect(Range<T> range)
Parameters
range
Range<T>The range with which to intersect.
Returns
- Range<T>
A new Range<T> representing the overlap between the two ranges, or null if there is no intersection.
Exceptions
- ArgumentNullException
Thrown if the provided range is null.
OnEquals(Range<T>)
Determines equality between this instance and another instance of the same type.
protected override bool OnEquals(Range<T> other)
Parameters
other
Range<T>An object to compare with this instance.
Returns
- bool
true if the objects are equal; otherwise, false.
Parse(string)
Parses the specified string representation and returns a new Range<T> instance.
public static Range<T> Parse(string value)
Parameters
value
stringThe string representation of the range.
Returns
Exceptions
- ArgumentNullException
Thrown if the input string is empty.
- ArgumentOutOfRangeException
Thrown if the string length is less than 3.
SubRange(T, T)
Creates a sub-range from the current range given the specified minimum and maximum values.
public Range<T> SubRange(T min, T max)
Parameters
min
TThe minimum value of the sub-range.
max
TThe maximum value of the sub-range.
Returns
Exceptions
- ArgumentException
Thrown if either min or max is not contained within the current range.
ToString()
public override string ToString()
Returns
Operators
explicit operator Range<T>(string)
Defines an explicit conversion of a string representation to a Range<T>.
public static explicit operator Range<T>(string str)
Parameters
str
stringThe string representation of the range.
Returns
- Range<T>