當前位置: 首頁>>代碼示例>>C#>>正文


C# List<T>.FindIndex方法代碼示例

本文整理匯總了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));
   }
}
開發者ID:.NET開發者,項目名稱:System.Collections.Generic,代碼行數:57,代碼來源:List.FindIndex

輸出:

'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");
   }
}
開發者ID:.NET開發者,項目名稱:System.Collections.Generic,代碼行數:59,代碼來源:List.FindIndex

輸出:

'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));
   }
}
開發者ID:.NET開發者,項目名稱:System.Collections.Generic,代碼行數:57,代碼來源:List.FindIndex

輸出:

'J' starts at index 3
'Ju' starts at index 5


注:本文中的System.Collections.Generic.List<T>.FindIndex方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。