1

I have the following:

var a = new string[] {"a", "b", "c"};

How can I check this array to see if it contains "c"?

3 Answers 3

4

Try this one,

bool x = a.Contains("c");
Sign up to request clarification or add additional context in comments.

Comments

3

Use Contains extension method - a.Contains("c");

Comments

1

If you don't want to use the extension method of System.Linq.Enumerable, or if you fear that's performing too bad, the "old" way is the ugly

Array.IndexOf(a, "c") != -1

Another ugly possibility is to use one of the explicit interface implementations, like

((IList<string>)a).Contains("c")

In some ways, arrays have an obsolete feeling to them.

Comments

Your Answer

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