本文整理汇总了C#中Range.CompareTo方法的典型用法代码示例。如果您正苦于以下问题:C# Range.CompareTo方法的具体用法?C# Range.CompareTo怎么用?C# Range.CompareTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Range
的用法示例。
在下文中一共展示了Range.CompareTo方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RangeCompareToWithin
public void RangeCompareToWithin()
{
var range = new Range<int>(3, 10);
int value = 5;
int comp = range.CompareTo(value);
Assert.AreEqual(0, comp);
}
示例2: RangeCompareToAbove
public void RangeCompareToAbove()
{
var range = new Range<int>(3, 10);
int value = 12;
int comp = range.CompareTo(value);
Assert.AreEqual(1, comp);
}
示例3: RangeCompareToBelow
public void RangeCompareToBelow()
{
var range = new Range<int>(3, 10);
int value = 1;
int comp = range.CompareTo(value);
Assert.AreEqual(-1, comp);
}
示例4: Intersect
public static IntersectionResult Intersect(Range range1, Range range2)
{
var compareResult = range1.CompareTo(range2);
if (compareResult != 0)
{
return null;
}
if (range1.From > range2.From)
{
return Intersect(range2, range1);
}
// r1.From-r1.To-r2.From-r2-To
if (range1.To < range2.From)
{
return null;
}
// r1.From-r2.From-r1.To-r2.To
if (range1.From <= range2.From && range2.From <= range1.To && range1.To <= range2.To)
{
Range leftSetDifference = null;
var intersection = new Range() { From = Min(range1.To, range2.From), To = Max(range1.To, range2.From) };
Range rightSetDifference = null;
if (range1.From < intersection.From)
{
leftSetDifference = new Range() {From = range1.From, To = intersection.From - 1};
}
if (range2.To > intersection.To)
{
rightSetDifference = new Range() { From = intersection.To + 1, To = range2.To };
}
return new IntersectionResult()
{
Intersection = intersection,
LeftSetDifference = leftSetDifference,
RightSetDifference = rightSetDifference
};
}
// r1.From-r2.From-r2.To-r2.1rom
if (range1.From <= range2.From && range2.To <= range1.To)
{
Range leftSetDifference = null;
var intersection = new Range() { From = Max(range1.From, range2.From), To = Min(range1.To, range2.To) };
Range rightSetDifference = null;
if (range1.From < intersection.From)
{
leftSetDifference = new Range() { From = range1.From, To = intersection.From - 1 };
}
if (range1.To > intersection.To)
{
rightSetDifference = new Range() { From = intersection.To + 1, To = range1.To };
}
return new IntersectionResult()
{
Intersection = intersection,
LeftSetDifference = leftSetDifference,
RightSetDifference = rightSetDifference
};
}
throw new NotSupportedException(
string.Format(
"The ranges (From: {0}, To: {1}) and (From: {2}, To: {3}) are not supported to intersect",
range1.From, range1.To, range2.From, range2.To));
}