string array.Contains?

前端 未结 10 1446
无人及你
无人及你 2021-02-07 03:22

.NET 2

string[] myStrings = GetMyStrings();    
string test = \"testValue\";

How can I verify if myStrings contains test

10条回答
  •  猫巷女王i
    2021-02-07 04:08

    In .NET 2.0, you could do the following if you want the index:

    int index = Array.FindIndex(
        myStrings,
        delegate(string s) { return s.Equals(test); }
    );
    

    index will be -1 if myStrings does not contain test.

    If you merely want to check for existence:

    bool exists = Array.Exists(
        myStrings,
        delegate(string s) { return s.Equals(test); }
    );
    

提交回复
热议问题