本文整理汇总了C#中SortedList.BinarySearch方法的典型用法代码示例。如果您正苦于以下问题:C# SortedList.BinarySearch方法的具体用法?C# SortedList.BinarySearch怎么用?C# SortedList.BinarySearch使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SortedList
的用法示例。
在下文中一共展示了SortedList.BinarySearch方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestBinarySearch
public void TestBinarySearch()
{
Assert.Throws<ArgumentNullException>(() => { int i1, i2; CollectionExtensions.BinarySearch<string, string>(null, null, out i1, out i2); });
Assert.Throws<ArgumentNullException>(() => { int i1, i2; CollectionExtensions.BinarySearch<string, string>(new SortedList<string, string>(), null, out i1, out i2); });
Assert.Throws<ArgumentNullException>(() => { int i1, i2; CollectionExtensions.BinarySearch<string, string>(null, "", out i1, out i2); });
Assert.DoesNotThrow(() => { int i1, i2; CollectionExtensions.BinarySearch<string, string>(new SortedList<string, string>(), "", out i1, out i2); });
SortedList<int, string> list = new SortedList<int, string>();
Action<int, int, int> assert = (int key, int index1, int index2) =>
{
int i1, i2;
list.BinarySearch(key, out i1, out i2);
Assert.AreEqual(index1, i1);
Assert.AreEqual(index2, i2);
};
assert(5, int.MinValue, int.MaxValue);
list.Add(5, "five"); // 5
assert(4, int.MinValue, 0);
assert(5, 0, 0);
assert(6, 0, int.MaxValue);
list.Add(6, "six"); // 5, 6
assert(4, int.MinValue, 0);
assert(5, 0, 0);
assert(6, 1, 1);
assert(7, 1, int.MaxValue);
list.Add(3, "three"); // 3, 5, 6
assert(2, int.MinValue, 0);
assert(3, 0, 0);
assert(4, 0, 1);
assert(5, 1, 1);
assert(6, 2, 2);
assert(7, 2, int.MaxValue);
list.Add(15, "fifteen"); // 3, 5, 6, 15
assert(2, int.MinValue, 0);
assert(3, 0, 0);
assert(4, 0, 1);
assert(5, 1, 1);
assert(6, 2, 2);
assert(7, 2, 3);
assert(11, 2, 3);
assert(14, 2, 3);
assert(15, 3, 3);
assert(16, 3, int.MaxValue);
assert(-999999999, int.MinValue, 0);
assert(999999999, 3, int.MaxValue);
}