List
学习了list的几个方法
list.findIndex(Predict<T> match);
下面这几个是源码反编译的方法。
/// <summary>Represents the method that defines a set of criteria and determines whether the specified object meets those criteria.</summary>
/// <param name="obj">The object to compare against the criteria defined within the method represented by this delegate.</param>
/// <typeparam name="T">The type of the object to compare.</typeparam>
/// <returns>
/// <see langword="true" /> if <paramref name="obj" /> meets the criteria defined within the method represented by this delegate; otherwise, <see langword="false" />.</returns>
public delegate bool Predicate<in T>(T obj);
/// <summary>Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the entire <see cref="T:System.Collections.Generic.List`1" />.</summary>
/// <param name="match">The <see cref="T:System.Predicate`1" /> delegate that defines the conditions of the element to search for.</param>
/// <returns>The zero-based index of the first occurrence of an element that matches the conditions defined by <paramref name="match" />, if found; otherwise, -1.</returns>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="match" /> is <see langword="null" />.</exception>
public int FindIndex(Predicate<T> match);
/// <summary>Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the range of elements in the <see cref="T:System.Collections.Generic.List`1" /> that starts at the specified index and contains the specified number of elements.</summary>
/// <param name="startIndex">The zero-based starting index of the search.</param>
/// <param name="count">The number of elements in the section to search.</param>
/// <param name="match">The <see cref="T:System.Predicate`1" /> delegate that defines the conditions of the element to search for.</param>
/// <returns>The zero-based index of the first occurrence of an element that matches the conditions defined by <paramref name="match" />, if found; otherwise, -1.</returns>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="match" /> is <see langword="null" />.</exception>
/// <exception cref="T:System.ArgumentOutOfRangeException">
/// <paramref name="startIndex" /> is outside the range of valid indexes for the <see cref="T:System.Collections.Generic.List`1" />.
/// -or-
/// <paramref name="count" /> is less than 0.
/// -or-
/// <paramref name="startIndex" /> and <paramref name="count" /> do not specify a valid section in the <see cref="T:System.Collections.Generic.List`1" />.</exception>
public int FindIndex(int startIndex, int count, Predicate<T> match);
/// <summary>Searches for an element that matches the conditions defined by the specified predicate, and returns the last occurrence within the entire <see cref="T:System.Collections.Generic.List`1" />.</summary>
/// <param name="match">The <see cref="T:System.Predicate`1" /> delegate that defines the conditions of the element to search for.</param>
/// <returns>The last element that matches the conditions defined by the specified predicate, if found; otherwise, the default value for type <paramref name="T" />.</returns>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="match" /> is <see langword="null" />.</exception>
[return: MaybeNull]
public T FindLast(Predicate<T> match);
/// <summary>Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the last occurrence within the entire <see cref="T:System.Collections.Generic.List`1" />.</summary>
/// <param name="match">The <see cref="T:System.Predicate`1" /> delegate that defines the conditions of the element to search for.</param>
/// <returns>The zero-based index of the last occurrence of an element that matches the conditions defined by <paramref name="match" />, if found; otherwise, -1.</returns>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="match" /> is <see langword="null" />.</exception>
public int FindLastIndex(Predicate<T> match);
实现
public class Racer
{
private string _country;
public string Country
{
get => _country;
set => _country = value;
}
public Racer(string country)
{
_country = country;
}
}
public class FindCountry
{
private string _country;
public FindCountry(string country) => _country = country;
public bool FindCountryPredicate(Racer racer) => racer?.Country == _country;
}
static void Main(string[] args)
{
List<Racer> racers=new List<Racer>();
racers.Add(new Racer("China"));
racers.Add(new Racer("USA"));
racers.Add(new Racer("JAPAN"));
racers.Add(new Racer("USA"));
int index=racers.FindIndex(new FindCountry("JAPAN").FindCountryPredicate);
var rac = racers.FindAll(new FindCountry("USA").FindCountryPredicate);
if(rac!=null)
Console.WriteLine(rac.Count);
else
{
Console.WriteLine("not found");
}
Console.ReadLine();
}
对每个list中的子项进行处理,也是使用的委托
/// <summary>Performs the specified action on each element of the <see cref="T:System.Collections.Generic.List`1" />.</summary>
/// <param name="action">The <see cref="T:System.Action`1" /> delegate to perform on each element of the <see cref="T:System.Collections.Generic.List`1" />.</param>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="action" /> is <see langword="null" />.</exception>
/// <exception cref="T:System.InvalidOperationException">An element in the collection has been modified.</exception>
public void ForEach(Action<T> action);
racers.ForEach(new FindCountry("").ChangeNameForEach);
racers.ForEach(r=>r.Country="in earch"+r.Country);
public void ChangeNameForEach(Racer racer) => racer.Country += " in earch";
也可以直接使用lamba表达式
var rac1 = racers.Find(r => r.Country == "USA");