Answer the question
In order to leave comments, you need to log in
C# - Is it possible (how) to use "||" in an array?
Hello! I am a beginner C# programmer. I was writing a program, and I came across this problem:
There are several "strings" - different variations of people's names. Each name has its own array with its variations (For example: Vasya, Vasily, VASYA, VASILY, etc.). It is necessary to use the logical "Or" - "||" in the array. But when I use it, I get an error - "The operator "||" cannot be applied to an operand of type "string" and "string"". Is it possible to solve this problem? And if so, how? Please, help.
Answer the question
In order to leave comments, you need to log in
No, the II operator cannot be used in this way. However, you can "pump" an array using extension methods.
static class Program
{
static bool HasName(this IEnumerable<string> list, string name)
{
return list.Any(n => string.Compare(n, name, StringComparison.OrdinalIgnoreCase) == 0);
}
static void Main(string[] args)
{
var list = new string[] {"Вася", "Василий", "Вась"};
var name = "ВАСЯ";
if (list.HasName(name))
Console.WriteLine("Совпало");
else
Console.WriteLine("Ошибка");
Console.ReadLine();
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question