C# List 详解五

这篇具有很好参考价值的文章主要介绍了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();
        }
    }
}

运行:

C# List 详解五,C#,c#

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();
        }
    }
}

运行:

C# List 详解五,C#,c#

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();
        }
    }
}

运行:

C# List 详解五,C#,c#

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();
        }
    }
}

运行:

C# List 详解五,C#,c#

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();
        }
    }
}

运行:

C# List 详解五,C#,c#

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();
        }
    }
}

运行:

C# List 详解五,C#,c#

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();
        }
    }
}

运行:

C# List 详解五,C#,c#

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();
        }
    }
}

运行:

 C# List 详解五,C#,c#

第 5 / 7  篇  End文章来源地址https://www.toymoban.com/news/detail-602891.html

到了这里,关于C# List 详解五的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • C# list<T>去重

    List object is int object is decimal object is char object is bool object is string List List 集合里有三条记录,其中两条重复。 使用Distinct后,还有三条,说明distinct失败 原因是,引用类型即使属性一样,引用地址是不一样的。 只能用别的方式去避免。

    2024年02月05日
    浏览(33)
  • c# List<T>.Aggregate

    ListT.Aggregate 方法的定义: 参数解析如下: TAccumulate seed :初始累积值,也是累积的起始值(默认值)。 FuncTAccumulate, T, TAccumulate func :累积计算的逻辑函数,接受两个参数,第一个参数是当前的累积值,第二个参数是集合中的元素,返回一个新的累积值。 seed :初始累积值是一

    2024年02月12日
    浏览(23)
  • C# 给List编个序号

    给List编个号

    2024年02月07日
    浏览(27)
  • C# 泛型List排序的实现

    本文主要介绍了C# 泛型List排序的实现,分享给大家,具体如下: 代码 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 9

    2024年02月12日
    浏览(27)
  • C# List 复制之深浅拷贝

    声明类 执行上述方法, 修改list1 中的属性值, 发现list2 的属性值会跟着变化, list3 的属性值不发生变化; 由于进行的是浅拷贝,所以直接将list1的内容复制给了list2,虽然list1和list2所在的地址不一样,但是两个list存储的对象仍然是相同的(因为把list1复制到list2时,浅拷贝复制的

    2024年02月07日
    浏览(29)
  • C# list的sort排序

    目录 前言: 值类型的排序: 方法一:直接调用sort函数 方法二:通过C# ling表达式与CompareTo接口配合使用 方法三:降序的实现 对于自定义类型的sort排序  方法一:通过实现IComparable接口重写CompareTo方法来排序 方法二:通过ling表达式实现          有时需要对List列表中内

    2024年02月15日
    浏览(25)
  • C#简单操作:C#中List常用方法 判断存在、查找、排序

    目常用List来进行数据操作管理,有一些方法经常百度,所以这里记录下。 目录 1. List判断元素是否存在,返回bool 2. List查找,返回对象 3. List排序 4. 对象属性打印 5. List 其他方法  

    2024年02月11日
    浏览(33)
  • C#(五十八)之C#List

    前几天,看同事写的代码中有list相关的字眼,百度了一下,原来是C#中list泛型集合。 了解一下。 List:泛型集合,ListT类是 ArrayList 类的泛型等效类。该类使用大小可按需动态增加的数组实现 IListT 泛型接口。  泛型的好处: 它为使用c#语言编写面向对象程序增加了极大的

    2024年02月15日
    浏览(25)
  • 【C# 基础精讲】List 集合的使用

    在C#中, ListT 是一种非常常用的泛型集合类,用于存储一组相同类型的元素。 ListT 具有动态调整大小的能力,可以方便地添加、删除、查找和修改元素,非常灵活和高效。本文将详细介绍 ListT 集合的使用方法,包括创建 ListT 对象、添加元素、删除元素、查找元素、遍历集合

    2024年02月06日
    浏览(23)
  • C# List去掉某个位置的元素

    在 C# 中,可以使用 RemoveAt 方法从 List 中删除指定位置的元素。这个方法接受一个整数参数,表示要删除的元素的索引。 以下是一些示例代码,展示如何使用 RemoveAt 方法从 List 中删除指定位置的元素: 在这个示例中,我们首先创建了一个包含一些字符串的 Liststring 对象。然

    2024年02月16日
    浏览(26)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包