
- 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# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# OOP & Data Handling
- 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# ArrayList - BinarySearch() Method
The C# ArrayList BinarySearch() method is used to search a specific element efficiently within a sorted ArrayList.
This method performs a binary search, which is much faster than a linear search, but it requires the ArrayList Sorted.
Syntax
Following are the syntax of the C# ArrayList BinarySearch() method −
public virtual int BinarySearch(object value); public virtual int BinarySearch(object value, IComparer comparer); public virtual int BinarySearch(int index, int count, object value, IComparer comparer);
Parameters
This method accepts the following parameters −
- value: It is required to specify the object to search for in the ArrayList. It can be null for reference types.
- comparer: It is optional, which Icomparer implementation to customize the comparison.
- index: It is a zero-based starting index of the range to search.
- count: It represents the number of elements in the range to search.
Return value
This method returns a zero-based index of the value in the sorted ArrayList if the value is found. Otherwise, a negative number.
Example 1: Add Elements in Range
This is the basic example of the ArrayList collection to demonstrate how to use BinarySearch to search a specific object in the ArrayList −
using System; using System.Collections; class Program { static void Main() { ArrayList arrayList = new ArrayList { 10, 20, 30, 40, 50 }; // Perform BinarySearch int index = arrayList.BinarySearch(30); if (index >= 0) { Console.WriteLine($"Element found at index: {index}"); } else { Console.WriteLine("Element not found"); } } }
Output
Following is the output −
Element found at index: 2
Example 2: What if The ArrayList is Not Sorted
Let us see another example of the BinarySearch() method. If the ArrayList is not sorted, we first need to sort it before performing the search operation −
using System; using System.Collections; class Program { static void Main() { ArrayList arrayList = new ArrayList { 10, 30, 20, 60, 40, 15 }; arrayList.Sort(); // Perform BinarySearch int index = arrayList.BinarySearch(30); if (index >= 0) { Console.WriteLine($"Element found at index: {index}"); } else { Console.WriteLine("Element not found"); } } }
Output
Following is the output −
Element found at index: 3
Example 3: Using a Custom Comparer
In this example, we use the BinarySearch method with custom comparer to find the specific elements from the ArrayList −
using System; using System.Collections; class DescendingComparer: IComparer { public int Compare(object x, object y) { // reverse order comparsion return Comparer.Default.Compare(y, x); } } class Program { static void Main() { // Create and sort the ArrayList in descending order ArrayList arrayList = new ArrayList { 50, 40, 30, 20, 10 }; arrayList.Sort(new DescendingComparer()); // Perform BinarySearch using the custom comparer int index = arrayList.BinarySearch(30, new DescendingComparer()); if (index >= 0) { Console.WriteLine($"Element found at index: {index}"); } else { Console.WriteLine("Element not found"); } } }
Output
Following is the output −
Element found at index: 2
Example 4: Searching a Range of Elements
In this example, we use the BinarySearch() method to search an element with the range −
using System; using System.Collections; class Program { static void Main() { // Create and sort the ArrayList ArrayList arrayList = new ArrayList { 10, 20, 30, 40, 50 }; arrayList.Sort(); // Perform BinarySearch in a specific range int index = arrayList.BinarySearch(1, 3, 40, null); if (index >= 0) { Console.WriteLine($"Element found at index: {index}"); } else { Console.WriteLine("Element not found in the range"); } } }
Output
Following is the output −
Element found at index: 3