简体   繁体   中英

How can I check to see if a C# string array contains a value

I have the following:

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

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

试试这个

bool x = a.Contains("c");

使用Contains扩展方法a.Contains("c");

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM