简体   繁体   中英

Check if a string list contains an item

I have a string list like;

public static readonly List<string> myList = new List<string>
{
    "123",
    "456",
    "789"
};

And I have a method, let's say MethodX(string). MethodX(string) is looking for and compares entered string in another string list that comes from API. It returns true if an entered string matches any string from the list.

Now what I'm doing is;

var y = myList;
foreach (var x in y)
{
    var asd = me.MethodX(x);
    if (asd == true)
    {
        // Do stuff
    }
}

I want every member of the string list to be entered MethodX() and check if it's true. But currently it only tries "123" as the first member of myList.

How can I make it happen?

bool MethodX(this List<string>,string x)
{
   return this.Any(s=>s==x);
}

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