简体   繁体   中英

How to determine whether a value is in a List<string>?

In C# using VS2005, let's say I have a List<string> called listOfStrings and a string variable called str .

Can I do something like this?

if (str in listOfStrings) { ... }

Yes, you can. Have a look at the List<T>.Contains(..) method.

List<string> l = new List<string>();
l.Add("Hello");
l.Add("World");

if (l.Contains("Hello"))
{
    // ..
}

You can use List.Contains() to do this.

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