C# List 详解四

目录

18.FindLast(Predicate)    

19.FindLastIndex(Int32, Int32, Predicate)    

20.FindLastIndex(Int32, Predicate)    

21.FindLastIndex(Predicate)    

22.ForEach(Action)    

23.GetEnumerator()    

24.GetHashCode()    

25.GetRange(Int32, Int32)    


C# List 详解一

1.Add(T),2.AddRange(IEnumerable),3.AsReadOnly(),4.BinarySearch(T),

C# List 详解二

5.Clear(),6.Contains(T),7.ConvertAll(Converter),8.CopyTo(Int32, T[], Int32, Int32),9.CopyTo(T[]),10.CopyTo(T[], Int32)

C# List 详解三

11.Equals(Object),12.Exists(Predicate),13.Find(Predicate),14.FindAll(Predicate),15.FindIndex(Int32, Int32, Predicate),16.FindIndex(Int32, Predicate),17.FindIndex(Predicate)  

C# List 详解四

18.FindLast(Predicate),19.FindLastIndex(Int32, Int32, Predicate),20.FindLastIndex(Int32, Predicate),21.FindLastIndex(Predicate),22.ForEach(Action),23.GetEnumerator(),24.GetHashCode(),25.GetRange(Int32, Int32) 

C# List 详解五

26.GetType(),27.IndexOf(T),28.IndexOf(T, Int32),29.IndexOf(T, Int32, Int32),30.Insert(Int32, T),31.InsertRange(Int32, IEnumerable),32.LastIndexOf(T),33.LastIndexOf(T, Int32),34.LastIndexOf(T, Int32, Int32)    

C# List 详解六

35.MemberwiseClone(),36.Remove(T),37.RemoveAll(Predicate),38.RemoveAt(Int32),39.RemoveRange(Int32, Int32),40.Reverse(),41.Reverse(Int32, Int32)    

C# List 详解七

42.Sort(),43.ToArray(),44.ToString(),45.TrimExcess(),46.TrueForAll(Predicate) 

C# List 详解一_熊思宇的博客-CSDN博客

C# List 详解二_熊思宇的博客-CSDN博客

C# List 详解三_熊思宇的博客-CSDN博客

C# List 详解五_熊思宇的博客-CSDN博客

C# List 详解六_熊思宇的博客-CSDN博客

C# List 详解七_熊思宇的博客-CSDN博客

18.FindLast(Predicate<T>)    

搜索与指定谓词所定义的条件相匹配的元素,并返回整个 List<T> 中的最后一个匹配元素。

public T? FindLast (Predicate<T> match);

参数
match
Predicate<T>
Predicate<T> 委托,用于定义要搜索的元素的条件。

返回
T
如果找到,则为与指定谓词所定义的条件相匹配的最后一个元素;否则为类型 T 的默认值。

案例:

using System;
using System.Collections.Generic;

namespace ListTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            List<int> list1 = new List<int>() { 1, 3, 5, 5, 6 };
            int value = list1.FindLast(x => x == 5);
            Console.WriteLine("值:{0}", value);

            Console.ReadKey();
        }
    }
}

运行:

19.FindLastIndex(Int32, Int32, Predicate<T>)    

搜索与指定谓词所定义的条件相匹配的元素,并返回 List<T> 中包含指定元素个数、到指定索引结束的元素范围内最后一个匹配项的从零开始的索引。

public int FindLastIndex (int startIndex, int count, Predicate<T> match);

参数
startIndex
Int32
向后搜索的从零开始的起始索引。

count
Int32
要搜索的部分中的元素数。

match
Predicate<T>
Predicate<T> 委托,用于定义要搜索的元素的条件。

返回
Int32
如果找到与 match 定义的条件相匹配的最后一个元素,则为该元素的从零开始的索引;否则为 -1。

案例:

using System;
using System.Collections.Generic;

namespace ListTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            List<int> list1 = new List<int>() { 1, 3, 5, 5, 6 };
            int index = list1.FindLastIndex(x => x == 5);
            Console.WriteLine("下标:{0}", index);

            Console.ReadKey();
        }
    }
}

运行:

20.FindLastIndex(Int32, Predicate<T>)    

搜索与由指定谓词定义的条件相匹配的元素,并返回 List<T> 中从第一个元素到指定索引的元素范围内最后一个匹配项的从零开始的索引。

public int FindLastIndex (int startIndex, Predicate<T> match);

参数
startIndex
Int32
向后搜索的从零开始的起始索引。

match
Predicate<T>
Predicate<T> 委托,用于定义要搜索的元素的条件。

返回
Int32
如果找到与 match 定义的条件相匹配的最后一个元素,则为该元素的从零开始的索引;否则为 -1。

案例:

using System;
using System.Collections.Generic;

namespace ListTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            List<int> list1 = new List<int>() { 1, 3, 5, 5, 6 };
            int index = list1.FindLastIndex(2, x => x == 5);
            Console.WriteLine("下标:{0}", index);

            Console.ReadKey();
        }
    }
}

运行:

21.FindLastIndex(Predicate<T>)    

搜索与指定谓词所定义的条件相匹配的元素,并返回整个 List<T> 中最后一个匹配元素的从零开始的索引。

public int FindLastIndex (Predicate<T> match);

参数
match
Predicate<T>
Predicate<T> 委托,用于定义要搜索的元素的条件。

返回
Int32
如果找到与 match 定义的条件相匹配的最后一个元素,则为该元素的从零开始的索引;否则为 -1。

案例:

using System;
using System.Collections.Generic;

namespace ListTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            List<int> list1 = new List<int>() { 1, 3, 5, 5, 6 };
            int index = list1.FindLastIndex(x => x == 5);
            Console.WriteLine("下标:{0}", index);

            Console.ReadKey();
        }
    }
}

运行:

22.ForEach(Action<T>)    

对 List<T> 的每个元素执行指定操作。

public void ForEach (Action<T> action);

参数
action
Action<T>
要对 List<T> 的每个元素执行的 Action<T> 委托。

案例:

using System;
using System.Collections.Generic;

namespace ListTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            List<string> names = new List<string>();
            names.Add("Bruce");
            names.Add("Alfred");
            names.Add("Tim");
            names.Add("Richard");
            names.ForEach(Print);

            Console.ReadKey();
        }

        static void Print(string s)
        {
            Console.WriteLine(s);
        }
    }
}

运行:

23.GetEnumerator()    

返回循环访问 List<T> 的枚举数。

public System.Collections.Generic.List<T>.Enumerator GetEnumerator ();

返回
List<T>.Enumerator
用于 List<T>.Enumerator 的 List<T>。

案例:

using System;
using System.Collections.Generic;

namespace ListTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            List<string> names = new List<string>();
            names.Add("Bruce");
            names.Add("Alfred");
            names.Add("Tim");
            names.Add("Richard");
            List<string>.Enumerator strings = names.GetEnumerator();

            Console.ReadKey();
        }
    }
}

24.GetHashCode()    

作为默认哈希函数。(继承自 Object)

public virtual int GetHashCode ();

返回
Int32
当前对象的哈希代码。


案例:

using System;
using System.Collections.Generic;

namespace ListTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            List<string> names = new List<string>();
            names.Add("Bruce");
            names.Add("Alfred");
            names.Add("Tim");
            names.Add("Richard");
            int code = names.GetHashCode();
            Console.WriteLine(code);

            Console.ReadKey();
        }
    }
}

运行:

25.GetRange(Int32, Int32)    

在源 List<T> 中创建元素范围的浅表复制。

public System.Collections.Generic.List<T> GetRange (int index, int count);

参数
index
Int32
范围开始处的从零开始的 List<T> 索引。

count
Int32
范围中的元素数。

返回
List<T>
源 List<T> 中的元素范围的浅表副本复制。

案例:

using System;
using System.Collections.Generic;

namespace ListTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            List<int> list1 = new List<int>() { 1, 3, 5, 5, 6 };
            List<int> list2 = list1.GetRange(1, 2);
            Console.WriteLine(string.Join("-", list2));

            Console.ReadKey();
        }
    }
}

运行:

第 4 / 7  篇  End

猜你喜欢

转载自blog.csdn.net/qq_38693757/article/details/131723357