1

I have declared a string array and I want to compare with name given by user,

string[] MasterList = new string[] {
  "Askay", "Puram", "Raman", "Srinivasa",
  "Gopal", "Rajesh", "Anju", "Nagara",
};

string YourName;
Console.WriteLine("Enter your name: ");
YourName = Console.ReadLine();

for(i=0; i<5; i++)
{
    a = String.Compare(MasterList[i], YourName);
    Console.WriteLine("Your name is not among the list")
}

The resulting output is not what I am expecting, any ideas how I can go about it?

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109

3 Answers3

2

Why not use the Contains method?

Add the following line to your using directives first:

using System.Linq;

And then, remove the for loop and use the following line instead:

if (!MasterList.Contains(YourName, StringComparer.OrdinalIgnoreCase))
{
    Console.WriteLine("Your name is not among the list")
}
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
  • I think using `Contains` here is not safe, even it `Contains`, still may not be the name, should be fully matched – LONG Jul 06 '17 at 14:28
2

Why not Contains?

  string[] MasterList = new string[] {
    "Askay", "Puram", "Raman", "Srinivasa",
    "Gopal", "Rajesh", "Anju", "Nagara",
  };

  Console.WriteLine("Enter your name: ");
  string YourName = Console.ReadLine();

  // StringComparer.OrdinalIgnoreCase if you want to ignore case
  // MasterList.Contains(YourName) if you want case sensitive 
  if (!MasterList.Contains(YourName, StringComparer.OrdinalIgnoreCase))
    Console.WriteLine("Your name is not among the list")
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
-1
bool found = false;
foreach (string s in MasterList)
{
    if(s == YourName)
    found = true;
}
if(found)
    Console.WriteLine("Your name is among the list");
else
    Console.WriteLine("Your name is not among the list");
Peter Meadley
  • 509
  • 2
  • 16
  • 1
    Can people who are downvoting please give some comments to @Peter who is a relatively new user :) – garfbradaz Jul 06 '17 at 14:28
  • 1
    @garfbradaz if Peter cannot work out what is wrong with his answer there is always [How to write a good answer](https://stackoverflow.com/help/how-to-answer) – Jamiec Jul 06 '17 at 14:29
  • Thats what I mean @Jamiec - that is helpful, giving a link for guidance (I was trying to find it myself to post) - kudo! :) – garfbradaz Jul 06 '17 at 14:30
  • 1
    Comparing strings with `==` operators can yield unpredictable results (I did not downvote). You're referencing to see if it's the _exact same object_ (which strings often are not, regardless of contents). – gravity Jul 06 '17 at 14:30
  • 1
    @gravity see https://stackoverflow.com/questions/1659097/why-would-you-use-string-equals-over – Peter Meadley Jul 06 '17 at 14:31
  • @gravity Strings work differently in C# than they do in Java. `==` is safe. – Drew Kennedy Jul 06 '17 at 14:32
  • 1
    Thanks man...it worked just as i wanted it! thanks @Peter Meadley – Frederick Ottache Jul 06 '17 at 14:34
  • 1
    At very least break out of the loop when youve found the item. – Jamiec Jul 06 '17 at 14:35