Hi Guys
NP65 BinarySearch() method
Following simple program shows the way implementation of BinarySearch() method.
http://www.java2s.com/Tutorial/CSharp/0220__Data-Structure/MethodsDefinedbyArray.htm
But in the above website BinarySearch() method has been shown in the following way. What does this mean and how can this be implemented.
public static int BinarySearch(Array a, object v)
Anyone knows please explain the reason.
Thank you
using System;
public class BinarySearchDemo
{
public static void
{
int[] idNumbers = { 122, 167, 204, 219, 345 };
int entryId = 204;
int x = Array.BinarySearch(idNumbers, entryId);
Console.WriteLine("ID {0} position {1}", entryId, x);
}
}
Posted Nov 30, 2007, 5:34 PM
Thank you for the explanation, Alan.
AlanPosted Nov 30, 2007, 4:07 PM
To call a method in C#, you need to know a number of things about it:
1. Its name and accessibility - public, protected, private etc.
2. Whether its instance or static.
3. Its return type - void, int, string etc
4. The number, type and order of its parameters and whether they're ref, out, params or just 'by value' (the default).
5. In the case of a generic method, its type parameters(s) and any constraints thereon.
So the page you're looking at is just telling you the 'signatures' of the methods of the array class so that you know what is needed to call them in practice.
Posted Nov 30, 2007, 1:23 PM
So public static int BinarySearch(Array a, object v) is helping merely to explain the content within. It won’t provide any help to built-up relevant code x = Array.BinarySearch(idNumbers, entryId);
There are many methods like this in the web page. I mean one can’t built-up a relevant code from such a method.
Posted Nov 30, 2007, 10:54 AM
Thank you, Alan
AlanPosted Nov 30, 2007, 10:43 AM
This line is just how the BinarySearch method is declared in the Array class.
public static int BinarySearch(Array a, object v)
As it's static and returns an int, you'd call it in practice with a line like this:
int x = Array.BinarySearch(idNumbers, entryId);
So there's no inconsistency here.