
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
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
- Related Articles
- How to binarySearch in android listview?
- Class method vs static method in Python
- Collections.replaceAll() method and List.replaceAll() method in Java
- The isEmpty() method of CopyOnWriteArrayList method in Java
- The hashCode() method of CopyOnWriteArrayList method in Java
- The clone() method of CopyOnWriteArrayList method in Java
- Default method vs static method in an interface in Java?
- Difference between Method Overloading and Method Overriding in Java
- Difference between Method Overriding and Method Hiding in C#
- Reference to a static method using method references in Java8
- Reference to an instance method using method references in Java8
- How does Promise.all() method differs from Promise.allSettled() method in JavaScript?
- How does Promise.any() method differs from Promise.race() method in JavaScript?
- Integer.lowestOneBit() method in Java
- Integer.numberOfLeadingZeros() method in Java

Advertisements