どうも、ちょげ(@chogetarou)です。
リスト(List)の要素のインデックス(index)を後ろから検索して取得する方法を紹介します。
方法

リスト(List)の要素のインデックス(index)を後ろから検索して取得するには、LastIndexOf()を使います。
まず、リストからLastIndexOf()を呼び出します。
そして、LastIndexOf()の引数に要素を指定します。
//myList=対象のリスト, item=検索する要素
int index = myList.LastIndexOf(item);
上記のLastIndexOf()は、リストの要素のインデックスを後ろから検索して取得します。
使用例
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
List<string> nums = new List<string>() { "one", "three", "one", "two", "three", "two", "four", "three", "one", "five" };
int index = nums.LastIndexOf("three");
Console.WriteLine(index);
}
}
出力:
7
コメント