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

相关推荐
是木子啦19 小时前
wpf passwordbox控件 光标移到最后
c#·wpf
布伦鸽19 小时前
C# WPF DataGrid使用Observable<Observable<object>类型作为数据源
开发语言·c#·wpf
.NET修仙日记21 小时前
2025年ASP.NETMVC面试题库全解析
面试·职场和发展·c#·asp.net·mvc·面试题·asp.net mvc
weixin_4569042721 小时前
C# 串口通信完整教程 (.NET Framework 4.0)
网络·c#·.net
我笔记1 天前
.net过滤器和缓存
c#
夏子曦1 天前
C#内存管理深度解析:从栈堆原理到高性能编程实践
开发语言·c#
William_cl1 天前
C# MVC网页调试的方法
开发语言·c#·mvc
小小的技术员1 天前
C# 无实体生成JSON字符串
c#·json
L X..2 天前
Unity反射调用 ReactiveProperty<T>(泛型类型)内部方法时崩溃
unity·c#·游戏引擎·.net
缺点内向2 天前
C# 中 Excel 工作表打印前页面边距的设置方法
c#·.net·excel