C# | Array.BinarySearch(Array, Object, IComparer) Method Last Updated : 31 Jan, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report This method searches for a value in a one-dimensional sorted array using a specified IComparer interface. Syntax: public static int BinarySearch(Array arr, Object val, IComparer comparer) Parameters: arr : The one-dimensional sorted array in which the search will happen. val : The object value which is to search for. comparer : When comparing elements then the IComparer implementation is used. Return Value: It returns the index of the specified value in the specified array if the value is found otherwise it returns a negative number. There are different cases of return values as follows: If the value is not found and value is less than one or more elements in the array, the negative number returned is the bitwise complement of the index of the first element that is larger than value. If the value is not found and value is greater than all elements in the array, the negative number returned is the bitwise complement of (the index of the last element plus 1). If this method is called with a non-sorted array, the return value can be incorrect and a negative number could be returned, even if the value is present in the array. Exceptions: ArgumentNullException: If the array is null. RankException: If array is multidimensional. ArgumentException: If the range is less than lower bound OR length is less than 0. ArgumentException: If the comparer is null, and value is of a type that is not compatible with the elements of array. InvalidOperationException: If the comparer is null, value does not implement the IComparable interface, and the search encounters an element that does not implement the IComparable interface. Example 1: In this example, the array stores some string value and find some string value after sorting the array. csharp // C# program to demonstrate the // Array.BinarySearch(Array, // Object, IComparer) Method using System; class GFG { // Main Method public static void Main() { // initializes a new Array string[] arr = new string[5] { "ABCD", "IJKL", "XYZ", "EFGH", "MNOP"}; Console.WriteLine("The original Array"); // calling "display" function display(arr); Console.WriteLine("\nsorted array"); // sorting the Array Array.Sort(arr); display(arr); Console.WriteLine("\n1st call"); // search for object "EFGH" object obj1 = "EFGH"; // call the "FindObj" function FindObj(arr, obj1); Console.WriteLine("\n2nd call"); object obj2 = "ABCD"; FindObj(arr, obj2); } // find object method public static void FindObj(string[] Arr, object Obj) { int index = Array.BinarySearch(Arr, Obj, StringComparer.CurrentCulture); if (index < 0) { Console.WriteLine("The object {0} is not "+ "found\nNext larger object is at"+ " index {1}", Obj, ~index); } else { Console.WriteLine("The object {0} is at "+ "index {1}", Obj, index); } } // display method public static void display(string[] arr) { foreach(string g in arr) { Console.WriteLine(g); } } } Output: The original Array ABCD IJKL XYZ EFGH MNOP sorted array ABCD EFGH IJKL MNOP XYZ 1st call The object EFGH is at index 1 2nd call The object ABCD is at index 0 Example 2: csharp // C# program to demonstrate the // Array.BinarySearch(Array, // Object, IComparer) Method using System; class GFG { // Main Method public static void Main() { // initializes a new Array. Array arr = Array.CreateInstance(typeof(Int32), 5); // Array elements arr.SetValue(20, 0); arr.SetValue(10, 1); arr.SetValue(30, 2); arr.SetValue(40, 3); arr.SetValue(50, 4); Console.WriteLine("The original Array"); // calling "display" function display(arr); Console.WriteLine("\nsorted array"); // sorting the Array Array.Sort(arr); display(arr); Console.WriteLine("\n1st call"); // search for object 10 object obj1 = 10; // call the "FindObj" function FindObj(arr, obj1); Console.WriteLine("\n2nd call"); object obj2 = 60; FindObj(arr, obj2); } // find object method public static void FindObj(Array Arr, object Obj) { int index = Array.BinarySearch(Arr, Obj, StringComparer.CurrentCulture); if (index < 0) { Console.WriteLine("The object {0} is not found\nNext"+ " larger object is at index {1}", Obj, ~index); } else { Console.WriteLine("The object {0} is at index {1}", Obj, index); } } // display method public static void display(Array arr) { foreach(int g in arr) { Console.WriteLine(g); } } } Output: The original Array 20 10 30 40 50 sorted array 10 20 30 40 50 1st call The object 10 is at index 0 2nd call The object 60 is not found Next larger object is at index 5 Reference: https://docs.microsoft.com/en-us/dotnet/api/system.array.binarysearch?view=netframework-4.7.2#System_Array_BinarySearch_System_Array_System_Object_System_Collections_IComparer_ Comment More infoAdvertise with us Next Article C# | Array.BinarySearch(Array, Object, IComparer) Method S SoumikMondal Follow Improve Article Tags : Technical Scripter C# CSharp-method CSharp-Arrays Similar Reads Decorators in Python In Python, decorators are a powerful and flexible way to modify or extend the behavior of functions or methods, without changing their actual code. A decorator is essentially a function that takes another function as an argument and returns a new function with enhanced functionality. Decorators are 10 min read AVL Tree Data Structure An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. Balance Factor = left subtree height - right subtree heightFor a Balanced Tree(for every node): -1 ⤠Balance Factor ⤠1Example of an 4 min read What is a Neural Network? Neural networks are machine learning models that mimic the complex functions of the human brain. These models consist of interconnected nodes or neurons that process data, learn patterns and enable tasks such as pattern recognition and decision-making.In this article, we will explore the fundamental 12 min read ArrayList in Java Java ArrayList is a part of the collections framework and it is a class of java.util package. It provides us with dynamic-sized arrays in Java. The main advantage of ArrayList is that, unlike normal arrays, we don't need to mention the size when creating ArrayList. It automatically adjusts its capac 9 min read Read JSON file using Python The full form of JSON is JavaScript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called JSON. To use this feature, we import the JSON package in Pytho 4 min read Multithreading in Python This article covers the basics of multithreading in Python programming language. Just like multiprocessing , multithreading is a way of achieving multitasking. In multithreading, the concept of threads is used. Let us first understand the concept of thread in computer architecture. What is a Process 8 min read Two Pointers Technique Two pointers is really an easy and effective technique that is typically used for Two Sum in Sorted Arrays, Closest Two Sum, Three Sum, Four Sum, Trapping Rain Water and many other popular interview questions. Given a sorted array arr (sorted in ascending order) and a target, find if there exists an 11 min read Python Match Case Statement Introduced in Python 3.10, the match case statement offers a powerful mechanism for pattern matching in Python. It allows us to perform more expressive and readable conditional checks. Unlike traditional if-elif-else chains, which can become unwieldy with complex conditions, the match-case statement 7 min read C# Tutorial C# (pronounced "C-sharp") is a modern, versatile, object-oriented programming language developed by Microsoft in 2000 that runs on the .NET Framework. Whether you're creating Windows applications, diving into Unity game development, or working on enterprise solutions, C# is one of the top choices fo 4 min read Regression in machine learning Regression in machine learning refers to a supervised learning technique where the goal is to predict a continuous numerical value based on one or more independent features. It finds relationships between variables so that predictions can be made. we have two types of variables present in regression 5 min read Like