[英]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"? 如何检查此数组以查看其是否包含“ 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 如果您不想使用
System.Linq.Enumerable
的扩展方法,或者如果您担心它的性能太差,那么“旧的”方法就是丑陋的
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. 在某些方面,数组对它们有过时的感觉。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.