本文整理汇总了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