'Why the result of Exists function use == and != is not opposite? [closed]
string input = "1234";
string[] array = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
bool in_N1 = Array.Exists(array, x => x == input.Substring(0, 1));
bool in_N2 = Array.Exists(array, x => x == input.Substring(1, 1));
bool in_N3 = Array.Exists(array, x => x == input.Substring(2, 1));
bool in_N4 = Array.Exists(array, x => x == input.Substring(3, 1));
Console.WriteLine(input.Substring(0, 1) + in_N1);
Console.WriteLine(input.Substring(1, 1) + in_N2);
Console.WriteLine(input.Substring(2, 1) + in_N3);
Console.WriteLine(input.Substring(3, 1) + in_N4);
bool in_N5 = Array.Exists(array, x => x != input.Substring(0, 1));
bool in_N6 = Array.Exists(array, x => x != input.Substring(1, 1));
bool in_N7 = Array.Exists(array, x => x != input.Substring(2, 1));
bool in_N8 = Array.Exists(array, x => x != input.Substring(3, 1));
Console.WriteLine(input.Substring(0, 1) + in_N5);
Console.WriteLine(input.Substring(1, 1) + in_N6);
Console.WriteLine(input.Substring(2, 1) + in_N7);
Console.WriteLine(input.Substring(3, 1) + in_N8);
//the result I got
1True
2True
3True
4True
1True
2True
3True
4True
ps.I also tried input with symbols like "+" or + "-" , the bool result is the same as what I thought. But if I input numbers there is the problem, I am confused...
Solution 1:[1]
Let's do a high-level English version of what you're asking C# to evaluate..
a) Dear C#, here are some numbers: 1,2,3,4. Does there exist any number in that set that is EQUAL to 1?
Yes
b) Dear C#, here are some numbers: 1,2,3,4. Does there exist any number in that set that is NOT EQUAL to 1?
Yes
In the first case, C# says yes because 1 is equal to 1. In the second case C# says yes because 2 is not equal to 1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
