c# - Array.Exist() only recognizing last element of an array -
i apologize if obvious answer, couldn't find related behavior on stackoverflow nor on google. i'm self-teaching c# , writing program involves use of username/password pairs. in case, using .exist()
determine if username in array of usernames.
static bool lookups(string targ, string[] arr) { if (array.exists(arr, e => e == targ)) { return true; } else { return false; } }
the argument targ
comes user, , argument arr
written program specify array should searched (this can use lookups
on different arrays). array i'm testing right derived text document. i've tested confirm transition .txt array working properly.
now actual issue. i've filled text document in question few values: "jupiter", "neptune", "saturn", "mars". if pass of first 3 lookups
, returns false
despite fact exist. part don't understand, however, if pass "mars" (or whatever last value happens be) function, returns true
should. if remove "mars" value, lookups
"saturn" true
not others. can offer explanation this? can post more complete picture of program if in identifying problem.
in comments mention split lines \n split separator. on windows \r\n used. hence split string array contain
- xxxx\r
- yyyy\r
- lastuser
the last line not contain newline work.
that explains why search in array finds last user in array. pass split operation not \r environment.newline.tochararray() remove newline characters in string array.
Comments
Post a Comment