How to get the index of an item in a list in a single step?

后端 未结 8 1061
旧巷少年郎
旧巷少年郎 2020-11-27 11:16

How can I find the index of an item in a list without looping through it?

Currently this doesn\'t look very nice - searching through the list for the same item twice

8条回答
  •  情歌与酒
    2020-11-27 11:46

    1. Simple solution to find index for any string value in the List.

    Here is code for List Of String:

    int indexOfValue = myList.FindIndex(a => a.Contains("insert value from list"));
    
    1. Simple solution to find index for any Integer value in the List.

    Here is Code for List Of Integer:

        int indexOfNumber = myList.IndexOf(/*insert number from list*/);
    

提交回复
热议问题