BinarySearch()는 숫자, 영숫자 또는 문자열에 관계없이 정렬된 목록에서 작동합니다. 요소의 인덱스를 찾습니다.
다음이 우리의 목록이라고 가정해 보겠습니다.
List<int> list = new List<int>(); list.Add(70); list.Add(150); list.Add(220); list.Add(250); list.Add(300);
이제 250이 있는 인덱스를 확인하려면 BinarySearch() 메서드를 사용하십시오.
list.BinarySearch(250);
예시
using System;
using System.Collections.Generic;
class Demo {
static void Main() {
List<int> list = new List<int>();
list.Add(70);
list.Add(150);
list.Add(220);
list.Add(250);
list.Add(300);
int value = list.BinarySearch(250);
Console.WriteLine("Element 250 at Index: "+value);
}
} 출력
Element 250 at Index: 3