![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
BinarySearch() method in C#
BinarySearch() works on a sorted list whether its numeric, alphanumeric or strings. It finds you the index of an element.
Let’s say the following is our list.
List<int> list = new List<int>(); list.Add(70); list.Add(150); list.Add(220); list.Add(250); list.Add(300);
Now to check the index where 250 is placed, use BinarySearch() method.
list.BinarySearch(250);
Example
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); } }
Output
Element 250 at Index: 3
Advertisements