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

后端 未结 8 1086
旧巷少年郎
旧巷少年郎 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:58

    For simple types you can use "IndexOf" :

    List arr = new List();
    arr.Add("aaa");
    arr.Add("bbb");
    arr.Add("ccc");
    int i = arr.IndexOf("bbb"); // RETURNS 1.
    

提交回复
热议问题