本文整理匯總了C#中System.Collections.Generic.List<T>.FindIndex方法的典型用法代碼示例。如果您正苦於以下問題:C# List<T>.FindIndex方法的具體用法?C# List<T>.FindIndex怎麽用?C# List<T>.FindIndex使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Collections.Generic.List<T>
的用法示例。
在下文中一共展示了List<T>.FindIndex方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: CompareTo
//引入命名空間
using System;
using System.Collections.Generic;
public class Employee : IComparable
{
public String Name { get; set; }
public int Id { get; set; }
public int CompareTo(Object o )
{
Employee e = o as Employee;
if (e == null)
throw new ArgumentException("o is not an Employee object.");
return Name.CompareTo(e.Name);
}
}
public class EmployeeSearch
{
String _s;
public EmployeeSearch(String s)
{
_s = s;
}
public bool StartsWith(Employee e)
{
return e.Name.StartsWith(_s, StringComparison.InvariantCultureIgnoreCase);
}
}
public class Example
{
public static void Main()
{
var employees = new List<Employee>();
employees.AddRange( new Employee[] { new Employee { Name = "Frank", Id = 2 },
new Employee { Name = "Jill", Id = 3 },
new Employee { Name = "Dave", Id = 5 },
new Employee { Name = "Jack", Id = 8 },
new Employee { Name = "Judith", Id = 12 },
new Employee { Name = "Robert", Id = 14 },
new Employee { Name = "Adam", Id = 1 } } );
employees.Sort();
var es = new EmployeeSearch("J");
Console.WriteLine("'J' starts at index {0}",
employees.FindIndex(es.StartsWith));
es = new EmployeeSearch("Ju");
Console.WriteLine("'Ju' starts at index {0}",
employees.FindIndex(es.StartsWith));
}
}
輸出:
'J' starts at index 3 'Ju' starts at index 5
示例2: CompareTo
//引入命名空間
using System;
using System.Collections.Generic;
public class Employee : IComparable
{
public String Name { get; set; }
public int Id { get; set; }
public int CompareTo(Object o )
{
Employee e = o as Employee;
if (e == null)
throw new ArgumentException("o is not an Employee object.");
return Name.CompareTo(e.Name);
}
}
public class EmployeeSearch
{
String _s;
public EmployeeSearch(String s)
{
_s = s;
}
public bool StartsWith(Employee e)
{
return e.Name.StartsWith(_s, StringComparison.InvariantCultureIgnoreCase);
}
}
public class Example
{
public static void Main()
{
var employees = new List<Employee>();
employees.AddRange( new Employee[] { new Employee { Name = "Frank", Id = 2 },
new Employee { Name = "Jill", Id = 3 },
new Employee { Name = "Dave", Id = 5 },
new Employee { Name = "Jack", Id = 8 },
new Employee { Name = "Judith", Id = 12 },
new Employee { Name = "Robert", Id = 14 },
new Employee { Name = "Adam", Id = 1 } } );
employees.Sort();
var es = new EmployeeSearch("J");
int index = employees.FindIndex(4, es.StartsWith);
Console.WriteLine("Starting index of'J': {0}",
index >= 0 ? index.ToString() : "Not found");
es = new EmployeeSearch("Ju");
index = employees.FindIndex(4, es.StartsWith);
Console.WriteLine("Starting index of 'Ju': {0}",
index >= 0 ? index.ToString() : "Not found");
}
}
輸出:
'J' starts at index 4 'Ju' starts at index 5
示例3: CompareTo
//引入命名空間
using System;
using System.Collections.Generic;
public class Employee : IComparable
{
public String Name { get; set; }
public int Id { get; set; }
public int CompareTo(Object o )
{
Employee e = o as Employee;
if (e == null)
throw new ArgumentException("o is not an Employee object.");
return Name.CompareTo(e.Name);
}
}
public class EmployeeSearch
{
String _s;
public EmployeeSearch(String s)
{
_s = s;
}
public bool StartsWith(Employee e)
{
return e.Name.StartsWith(_s, StringComparison.InvariantCultureIgnoreCase);
}
}
public class Example
{
public static void Main()
{
var employees = new List<Employee>();
employees.AddRange( new Employee[] { new Employee { Name = "Frank", Id = 2 },
new Employee { Name = "Jill", Id = 3 },
new Employee { Name = "Dave", Id = 5 },
new Employee { Name = "Jack", Id = 8 },
new Employee { Name = "Judith", Id = 12 },
new Employee { Name = "Robert", Id = 14 },
new Employee { Name = "Adam", Id = 1 } } );
employees.Sort();
var es = new EmployeeSearch("J");
Console.WriteLine("'J' starts at index {0}",
employees.FindIndex(0, employees.Count - 1, es.StartsWith));
es = new EmployeeSearch("Ju");
Console.WriteLine("'Ju' starts at index {0}",
employees.FindIndex(0, employees.Count - 1,es.StartsWith));
}
}
輸出:
'J' starts at index 3 'Ju' starts at index 5