12

I have an array with these 3 items:

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

and I have this string

var department = "Warranty Service Representative"

I have this condition that is suppose to test if the string department is not in the departmentArray

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

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

1
  • On top of the answers below, Any() may work. Commented Jan 26, 2016 at 20:40

3 Answers 3

25

Wouldn't this be simpler?

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

String department = "Warranty Service Representative";

if (departmentArray.Contains(department) == false)
{
}
Sign up to request clarification or add additional context in comments.

7 Comments

What assembly do I need to use Contains?
I get this error Error 1 'System.Array' does not contain a definition for 'Contains' and no extension method 'Contains' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)
You need to add using System.Linq; at the beginning of your program. (I'm surprised it isn't already there, it is a Linq Extension).
The <string> after Contains could also be excluded, as the compiler can derive what it should be.
What about something for a Dictionary lookup?
|
12
if (!Array.Exists(departmentArray, element => element == department))
{
}

In this case, underlying logic looks like:

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

While in your code, you have:

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

4 Comments

This will not work because in some cases I will have an array with 1 item and the string will match the one item in the array and will return true when I want that to return false
@user979331 Are you sure it will not work? Did you try it?
You might be confused by the expanded logic in the second part of my answer. Just try if (!Array.Exists(departmentArray, element => element == department)) instead of if (Array.Exists(departmentArray, element => element != department))
To try to explain it in "human speak," your code is saying "If any of the elements of the array do not equal the department..." This answer is looking for an element equal to department. The expression will return true if any are equal, or false if none are. By moving the ! to the beginning, it's saying "If none of the elements equal department," which is your expected result.
2

the code:

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

is true if exists any element in departmentArray != of department, that is why in this case the result is true instead of false (obviously there is strings in departmentArray different to department). So, if you want to check if department isn't in departmentArray try something like this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.