Click here to Skip to main content
13,195,710 members (55,390 online)
Rate this:
 
Please Sign up or sign in to vote.
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 20-Jan-13 20:39pm

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web03 | 2.8.171019.1 | Last Updated 21 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100