Click here to Skip to main content
15,879,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
class Program
{

static void Main(string[] args)
{
List<string> list = new List<string>();
list.Add("Amit");
list.Add("Raj");
list.Add("prem");
list.Add("Ram");
list.Add("pa");
foreach (string s in list)
{
Console.WriteLine(s);
}
Console.WriteLine(list.Capacity);
Console.WriteLine("Enter the name which you want to search: \t");
string a = Convert.ToString(Console.ReadLine());
int b = list.BinarySearch(a);
Console.WriteLine(b);
if (b >= 0)
{
Console.WriteLine("List is contains the Name");

}
else
{
Console.WriteLine("List is NOT contains the Name");
}

Console.ReadLine();
}
}

shows output:
Amit
Raj
Prem
Ram
pa
8
Enter the name which you want to search:
pa
-2
List is NOT contain the name



Please suggest some thing about this erro because that string is already in the list
explain the error.....Please
Posted

1 solution

See: http://msdn.microsoft.com/en-us/library/ftfdbfx6.aspx[^]
Quote:
The List<t> must already be sorted according to the comparer implementation; otherwise, the result is incorrect.

And you have forget to sort the list before searching.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900