C# List(リスト)の指定した値のインデックス番号を取得する

C#で、List(リスト)内にある値から、IndexOfを使用して、インデックス番号を取得するサンプルコードを記述してます。
環境
- OS windows10 pro 64bit
- Microsoft Visual Studio Community 2019 Version 16.7.1
IndexOf使い方
IndexOfを使用すると、指定した値のインデックス番号を取得することが可能です。
ここでは、作成したListの、値「one,two,three」のインデックス番号を取得してます。
using System;
using System.Collections.Generic;
using System.Linq;
namespace testapp
{
class Program
{
static void Main(string[] args)
{
var list = new List<string> { "one", "two", "three", "four", "five" };
// 結果
Console.WriteLine(list.IndexOf("one")); // 0
Console.WriteLine(list.IndexOf("two")); // 1
Console.WriteLine(list.IndexOf("three")); // 2
}
}
}
-
前の記事
javascript NaN (非数) であるかどうかを判定する 2020.12.16
-
次の記事
React.js ライブラリ「react-chrono」を使ってタイムラインを実装する 2020.12.17
コメントを書く