C# 7.0 List

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");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值