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 详解一

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博客

26.GetType()    

获取当前实例的 Type。(继承自 Object)

public Type GetType ();

返回
Type
当前实例的准确运行时类型。

案例:

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 };
            Type type = list1.GetType();

            Console.ReadKey();
        }
    }
}

27.IndexOf(T)    

搜索指定的对象,并返回整个 List<T> 中第一个匹配项的从零开始的索引。

public int IndexOf (T item);

参数
item
T
要在 List<T> 中定位的对象。 对于引用类型,该值可以为 null。

返回
Int32
如果找到,则为整个 item 中 List<T> 第一个匹配项的从零开始的索引;否则为 -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 value = list1.IndexOf(5);
            Console.WriteLine(value);

            Console.ReadKey();
        }
    }
}

运行:

28.IndexOf(T, Int32)    

搜索指定对象并返回 List<T> 中从指定索引到最后一个元素这部分元素中第一个匹配项的从零开始索引。

public int IndexOf (T item, int index);

参数
item
T
要在 List<T> 中定位的对象。 对于引用类型,该值可以为 null。

index
Int32
从零开始的搜索的起始索引。 空列表中 0(零)为有效值。

返回
Int32
如果在 List<T> 中从 index 到最后一个元素的元素范围内找到 item 的第一个匹配项,则为该项的从零开始的索引;否则为 -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 value = list1.IndexOf(5,3);
            Console.WriteLine(value);

            Console.ReadKey();
        }
    }
}

运行:

29.IndexOf(T, Int32, Int32)    

搜索指定对象并返回 List<T> 中从指定索引开始并包含指定元素数的这部分元素中第一个匹配项的从零开始索引。

public int IndexOf (T item, int index, int count);

参数
item
T
要在 List<T> 中定位的对象。 对于引用类型,该值可以为 null。

index
Int32
从零开始的搜索的起始索引。 空列表中 0(零)为有效值。

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

返回
Int32
如果在 List<T> 中从 index 开始并包含 count 个元素的元素范围内找到 item 的第一个匹配项,则为该项的从零开始的索引;否则为 -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 value = list1.IndexOf(5,3,2);
            Console.WriteLine(value);

            Console.ReadKey();
        }
    }
}

运行:

30.Insert(Int32, T)    

将元素插入 List<T> 的指定索引处。

public void Insert (int index, T item);

参数
index
Int32
应插入 item 的从零开始的索引。

item
T
要插入的对象。 对于引用类型,该值可以为 null。

案例:

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 };
            list1.Insert(1, 2);
            Console.WriteLine(string.Join("-", list1));

            Console.ReadKey();
        }
    }
}

运行:

31.InsertRange(Int32, IEnumerable<T>)    

将集合中的元素插入 List<T> 的指定索引处。

public void InsertRange (int index, System.Collections.Generic.IEnumerable<T> collection);

参数
index
Int32
应在此处插入新元素的从零开始的索引。

collection
IEnumerable<T>
应将其元素插入到 List<T> 中的集合。 集合自身不能为 null,但它可以包含为 null 的元素(如果类型 T 为引用类型)。

案例:

using System;
using System.Collections.Generic;

namespace ListTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string[] input = { "1", "2","3" };

            List<string> dinosaurs = new List<string>();
            dinosaurs.Add("a");
            dinosaurs.Add("b");
            dinosaurs.Add("c");
            dinosaurs.InsertRange(2, input);
            Console.WriteLine(string.Join("-", dinosaurs));

            Console.ReadKey();
        }
    }
}

运行:

32.LastIndexOf(T)    

搜索指定对象并返回整个 List<T> 中最后一个匹配项的从零开始索引。

public int LastIndexOf (T item);

参数
item
T
要在 List<T> 中定位的对象。 对于引用类型,该值可以为 null。

返回
Int32
如果在整个 List<T> 中找到 item 的最后一个匹配项,则为该项的从零开始的索引;否则为 -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.LastIndexOf(5);
            Console.WriteLine(index);

            Console.ReadKey();
        }
    }
}

运行:

33.LastIndexOf(T, Int32)    

搜索指定对象并返回 List<T> 中从第一个元素到指定索引这部分元素中最后一个匹配项的从零开始的索引。

public int LastIndexOf (T item, int index);

参数
item
T
要在 List<T> 中定位的对象。 对于引用类型,该值可以为 null。

index
Int32
向后搜索的从零开始的起始索引。

返回
Int32
如果找到,则返回在 List<T> 中从第一个元素到 index 的元素范围内找到 item 的最后一个匹配项的从零开始的索引;否则为 -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.LastIndexOf(5,2);
            Console.WriteLine(index);

            Console.ReadKey();
        }
    }
}

运行:

34.LastIndexOf(T, Int32, Int32)    

搜索指定对象并返回 List<T> 中到指定索引为止包含指定元素数的这部分元素中最后一个匹配项的从零开始索引。

public int LastIndexOf (T item, int index, int count);

参数
item
T
要在 List<T> 中定位的对象。 对于引用类型,该值可以为 null。

index
Int32
向后搜索的从零开始的起始索引。

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

返回
Int32
如果在 List<T> 中到 index 为止包含 count 个元素的这部分元素中找到 item 的最后一个匹配项,则为该项的从零开始的索引;否则为 -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.LastIndexOf(5,2,2);
            Console.WriteLine(index);

            Console.ReadKey();
        }
    }
}

运行:

 

第 5 / 7  篇  End

猜你喜欢

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