System.Collections.ArrayList.ArrayListAdapter.BinarySearch C# (CSharp) Method

BinarySearch() public method

public BinarySearch ( int index, int count, object value, IComparer comparer ) : int
index int
count int
value object
comparer IComparer
return int
			public override int BinarySearch(int index, int count, object value, IComparer comparer) 
			{
				int r, x, y, z;

				// Doing a direct BinarySearch on the adaptee will perform poorly if the adaptee is a linked-list.
				// Alternatives include copying the adaptee to a temporary array first.

				CheckRange(index, count, m_Adaptee.Count);

				if (comparer == null) 
				{
					comparer = Comparer.Default;
				}

				x = index;
				y = index + count - 1;

				while (x <= y) 
				{
					// Be careful with overflows
					z = x + ((y - x) / 2);
				
					r = comparer.Compare(value, m_Adaptee[z]);
				
					if (r < 0) 
					{
						y = z - 1;
					}
					else if (r > 0) 
					{
						x = z + 1;
					}	
					else 
					{
						return z;
					}
				}

				return ~x;
			}

Same methods

ArrayList.ArrayListAdapter::BinarySearch ( object value ) : int
ArrayList.ArrayListAdapter::BinarySearch ( object value, IComparer comparer ) : int