C# List 详解五

目录

26.GetType()

27.IndexOf(T)

[28.IndexOf(T, Int32)](#28.IndexOf(T, Int32))

[29.IndexOf(T, Int32, Int32)](#29.IndexOf(T, Int32, Int32))

[30.Insert(Int32, T)](#30.Insert(Int32, T))

[31.InsertRange(Int32, IEnumerable)](#31.InsertRange(Int32, IEnumerable))

32.LastIndexOf(T)

[33.LastIndexOf(T, Int32)](#33.LastIndexOf(T, Int32))

[34.LastIndexOf(T, Int32, 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)

cs 复制代码
public Type GetType ();

返回

Type

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

案例:

cs 复制代码
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> 中第一个匹配项的从零开始的索引。

cs 复制代码
public int IndexOf (T item);

参数

item

T

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

返回

Int32

如果找到,则为整个 item 中 List<T> 第一个匹配项的从零开始的索引;否则为 -1。

案例:

cs 复制代码
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> 中从指定索引到最后一个元素这部分元素中第一个匹配项的从零开始索引。

cs 复制代码
public int IndexOf (T item, int index);

参数

item

T

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

index

Int32

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

返回

Int32

如果在 List<T> 中从 index 到最后一个元素的元素范围内找到 item 的第一个匹配项,则为该项的从零开始的索引;否则为 -1。

案例:

cs 复制代码
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> 中从指定索引开始并包含指定元素数的这部分元素中第一个匹配项的从零开始索引。

cs 复制代码
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。

案例:

cs 复制代码
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> 的指定索引处。

cs 复制代码
public void Insert (int index, T item);

参数

index

Int32

应插入 item 的从零开始的索引。

item

T

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

案例:

cs 复制代码
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> 的指定索引处。

cs 复制代码
public void InsertRange (int index, System.Collections.Generic.IEnumerable<T> collection);

参数

index

Int32

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

collection

IEnumerable<T>

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

案例:

cs 复制代码
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> 中最后一个匹配项的从零开始索引。

cs 复制代码
public int LastIndexOf (T item);

参数

item

T

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

返回

Int32

如果在整个 List<T> 中找到 item 的最后一个匹配项,则为该项的从零开始的索引;否则为 -1。

案例:

cs 复制代码
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> 中从第一个元素到指定索引这部分元素中最后一个匹配项的从零开始的索引。

cs 复制代码
public int LastIndexOf (T item, int index);

参数

item

T

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

index

Int32

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

返回

Int32

如果找到,则返回在 List<T> 中从第一个元素到 index 的元素范围内找到 item 的最后一个匹配项的从零开始的索引;否则为 -1。

案例:

cs 复制代码
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> 中到指定索引为止包含指定元素数的这部分元素中最后一个匹配项的从零开始索引。

cs 复制代码
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。

案例:

cs 复制代码
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

相关推荐
猿长大人21 小时前
C# | JSON 序列化中的多态接口处理:基于 Attribute 实现精准 $type 控制
c#·json
一位狮子座的程序员1 天前
如何用RAG解决AI智能体的知识盲区?
开发语言·c#
Nemo_XP1 天前
C# gridlookupedit选中内容重复还原操作
服务器·前端·c#
猿长大人1 天前
C# | 函数式编程入门
开发语言·c#·.net
unityのkiven1 天前
C# 中的奇异递归模板模式:MonoSingleton<T> 的实现
java·开发语言·c#
雪隐1 天前
WPF + MVVM 实战系列02-告别 INPC 手写时代,做个体面的现代 WPF 人
前端·后端·c#
我是苏苏1 天前
WPF开发01:基础控件及常用模板篇
开发语言·c#·html·wpf
BerrySen1781 天前
现代 C# 全栈与高性能编程实战指南:从 CLR 底层机制到 AI Agent 智能体架构
人工智能·架构·c#
在世修行1 天前
从零打造 C# 工业视觉检测系统(六):OpenCV 图像处理与像素级比例尺标定
opencv·c#·视觉检测
小金子会发光2 天前
C# WinForms 手搓 Modbus RTU 调试助手:不依赖第三方 Modbus 库,从 CRC16 到 8 种功能码完整实现
c#·modbusrtu·串口通讯