c# indexof方法
C#List <T> .IndexOf()方法 (C# List<T>.IndexOf() Method)
List<T>.IndexOf() method is used to get the index of first occurrence of an element in the list.
List <T> .IndexOf()方法用于获取列表中元素首次出现的索引。
Syntax:
句法:
int List<T>.IndexOf(T item);
int List<T>.IndexOf(T item, int start_index);
int List<T>.IndexOf(T item, int start_index, int count);
Parameter:
参数:
item is an element of type T, whose first occurrence will be returned, if item found.
item是类型T的元素,如果找到item ,则将返回其第一个匹配项 。
start_index is the starting position from where you want to find the element in the list.
start_index是您要在列表中找到元素的起始位置。
count is the total elements from "start_index" to search the element in the list.
count是从“ start_index”开始搜索的元素总数。
Return value: It returns index of the element, if element founds in the list between specified indexes, if element does not found in the list – it returns -1.
返回值:它返回指数的元素,如果在指定索引的列表元素创立,如果元素没有在列表中找到-返回-1。
Example:
例:
int list declaration:
List<int> a = new List<int>();
adding elements:
a.Add(10);
a.Add(20);
a.Add(30);
a.Add(40);
a.Add(50);
a.Add(10);
a.Add(20);
a.Add(30);
a.Add(40);
a.Add(50);
Method calls:
a.IndexOf(20) //output: 1
a.IndexOf(100) //output: -1
a.IndexOf(20, 1) //output: 1
a.IndexOf(100, 1) //output: -1
a.IndexOf(20, 1, 3) //output: 1
a.IndexOf(20, 0, 1) //output: -1
C#示例使用List <T> .IndexOf()方法获取列表中元素的索引 (C# Example to get the index an element in the list using List<T>.IndexOf() Method)
using System;
using System.Text;
using System.Collections.Generic;
namespace Test
{
class Program
{
static void printList(List<int> lst)
{
//printing elements
foreach (int item in lst)
{
Console.Write(item + " ");
}
Console.WriteLine();
}
static void Main(string[] args)
{
//integer list
List<int> a = new List<int>();
//adding elements
a.Add(10);
a.Add(20);
a.Add(30);
a.Add(40);
a.Add(50);
a.Add(10);
a.Add(20);
a.Add(30);
a.Add(40);
a.Add(50);
//print the list
Console.WriteLine("list elements...");
printList(a);
//using List.IndexOf(T item)
//finding 20
int index = a.IndexOf(20);
if (index != -1)
Console.WriteLine("20 found at " + index + " position.");
else
Console.WriteLine("20 does not found in the list");
//finding 100
index = a.IndexOf(100);
if (index != -1)
Console.WriteLine("100 found at " + index + " position.");
else
Console.WriteLine("100 does not found in the list");
//using List.IndexOf(T item, int index)
//finding 20
index = a.IndexOf(20, 1); //start index is 1
if (index != -1)
Console.WriteLine("20 found at " + index + " position.");
else
Console.WriteLine("20 does not found in the list");
//finding 100
index = a.IndexOf(100, 1); //start index is 1
if (index != -1)
Console.WriteLine("100 found at " + index + " position.");
else
Console.WriteLine("100 does not found in the list");
//using List.IndexOf(T item, int start_index, int count)
//finding 20
//search will perform from index 1 to next 3 elements
index = a.IndexOf(20, 1, 3);
if (index != -1)
Console.WriteLine("20 found at " + index + " position.");
else
Console.WriteLine("20 does not found in the list");
//finding 20
//search will perform from index 0 to next 1 element
index = a.IndexOf(20, 0, 1);
if (index != -1)
Console.WriteLine("20 found at " + index + " position.");
else
Console.WriteLine("20 does not found in the list");
//hit ENTER to exit
Console.ReadLine();
}
}
}
Output
输出量
list elements...
10 20 30 40 50 10 20 30 40 50
20 found at 1 position.
100 does not found in the list
20 found at 1 position.
100 does not found in the list
20 found at 1 position.
20 does not found in the list
Reference: List<T>.IndexOf Method
翻译自: https://www.includehelp.com/dot-net/list-t-indexof-method-with-example-in-c-sharp.aspx
c# indexof方法