C # If the item is not in an array

I have an array with these three elements:

string[] departmentArray = {
    "Warranty Service Representative",
    "Warranty Service Administrative Manager",
    "Warranty and Site Administrator"
};

and i have this line

var department = "Warranty Service Representative"

I have this condition, which, suppose, is to check if there is a string department in departmentArray

if (Array.Exists(departmentArray, element => element != department)){
}

Obviously, the string is in an array, so it should return false, but this returns true for my string. What am I doing wrong?

+4
source share
3 answers

Wouldn't that be easier?

string[] departmentArray = { 
    "Warranty Service Representative", 
    "Warranty Service Administrative Manager", 
    "Warranty and Site Administrator" };

String department = "Warranty Service Representative";

if (departmentArray.Contains(department) == false)
{
}
+5
source
if (!Array.Exists(departmentArray, element => element == department))
{
}

In this case, the basic logic looks like this:

!(departmentArray[0] == department || departmentArray[1] == department || ..)

In your code, you have:

departmentArray[0] != department || departmentArray[1] != department || ..
+7
source

:

if (Array.Exists(departmentArray, element => element != department))
{
}

is true, departmentArray != of department, true false (, departmentArray department). , , department departmentArray, - :

bool IsNotInArray(String[] array, string element){
    return !Array.Exists(array, e => e == element);
}
+1

Source: https://habr.com/ru/post/1626058/


All Articles