C# List 详解二

这篇具有很好参考价值的文章主要介绍了C# List 详解二。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

目录

5.Clear()    

6.Contains(T)    

7.ConvertAll(Converter)    ,toutput>

8.CopyTo(Int32, T[], Int32, Int32)    

9.CopyTo(T[])    

10.CopyTo(T[], 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博客


5.Clear()    

从 List<T> 中移除所有元素。List 中比较常用的方法,用来清空 List 的所有元素。

public void Clear ();

案例:

using System;
using System.Collections.Generic;

namespace ListTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            List<string> dinosaurs = new List<string>();
            dinosaurs.Add("Pachycephalosaurus");
            dinosaurs.Add("Parasauralophus");
            dinosaurs.Add("Amargasaurus");

            Console.WriteLine(dinosaurs.Count);
            dinosaurs.Clear();
            Console.WriteLine(dinosaurs.Count);

            Console.ReadKey();
        }
    }
}

运行:

C# List 详解二,C#,c#

6.Contains(T)    

确定某元素是否在 List<T> 中。

public bool Contains (T item);

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

返回
Boolean
如果在 true 中找到 item,则为 List<T>;否则为 false。

案例:

using System;
using System.Collections.Generic;

namespace ListTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            List<string> dinosaurs = new List<string>();
            dinosaurs.Add("Pachycephalosaurus");
            dinosaurs.Add("Parasauralophus");
            dinosaurs.Add("Amargasaurus");

            if (dinosaurs.Contains("Amargasaurus"))
            {
                Console.WriteLine("包含");
            }
            else
            {
                Console.WriteLine("不包含");
            }

            Console.ReadKey();
        }
    }
}

运行:

C# List 详解二,C#,c#

7.ConvertAll<TOutput>(Converter<T,TOutput>)    

将当前 List<T> 中的元素转换为另一种类型,并返回包含已转换元素的列表。

public System.Collections.Generic.List<TOutput> ConvertAll<TOutput> (Converter<T,TOutput> converter);

类型参数
TOutput
目标数组元素的类型。

参数
converter
Converter<T,TOutput>
一个 Converter<TInput,TOutput> 委托,可将每个元素从一种类型转换为另一种类型。

返回
List<TOutput>
目标类型的 List<T>,包含当前 List<T> 中转换后的元素。

案例:

就是将 List 中的每一个元素转换为另一种格式,并返回一个新的 List 

using System;
using System.Collections.Generic;

namespace ListTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            List<int> intList = new List<int>() { 2, 5, 33, 6, 23 };
            List<string> stringList = intList.ConvertAll(new Converter<int, string>(convertall));
            Console.WriteLine(string.Join("-", stringList));

            Console.ReadKey();
        }

        static string convertall(int val)
        {
            return string.Format("'{0}'", val);
        }
    }
}

上面代码也可以用 Lambda 表达式去写:

using System;
using System.Collections.Generic;

namespace ListTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            List<int> intList = new List<int>() { 2, 5, 33, 6, 23 };
            List<string> stringList = intList.ConvertAll(new Converter<int, string>((val) => { return string.Format("'{0}'", val); }));
            Console.WriteLine(string.Join("-", stringList));

            Console.ReadKey();
        }
    }
}

运行:

C# List 详解二,C#,c#

8.CopyTo(Int32, T[], Int32, Int32)    

从目标数组的指定索引处开始,将元素的范围从 List<T> 复制到兼容的一维数组。

public void CopyTo (int index, T[] array, int arrayIndex, int count);

参数
index
Int32
复制即从源 List<T> 中从零开始的索引开始。

array
T[]
一维 Array,它是从 List<T> 复制的元素的目标。 Array 必须具有从零开始的索引。

arrayIndex
Int32
array 中从零开始的索引,从此处开始复制。

count
Int32
要复制的元素数。

由于引用类型在改变值后,其他的数组中的值也会改变,所以有时候,数组不得不用拷贝的方式,下面用两个案例来演示 CopyTo 的用法。

案例1:完整的拷贝

using System;
using System.Collections.Generic;

namespace ListTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            List<int> intList = new List<int>() { 2, 5, 33, 6, 23 };
            int[] list = new int[intList.Count];
            intList.CopyTo(0, list, 0, intList.Count);
            Console.WriteLine(string.Join("-", list));

            Console.ReadKey();
        }
    }
}

运行:

C# List 详解二,C#,c#

案例2:拷贝一部分

在下面这几个参数中,第一个参数 2 表示从 intList 中的下标 2 开始,即 33 开始,第三个参数 0 表示 list 数组中,从 0 这个下标开始赋值,往后赋值2个参数

using System;
using System.Collections.Generic;

namespace ListTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            List<int> intList = new List<int>() { 2, 5, 33, 6, 23, 45,36,38 };
            int[] list = new int[intList.Count];
            intList.CopyTo(2, list, 0, 3);
            Console.WriteLine(string.Join("-", list));

            Console.ReadKey();
        }
    }
}

 运行:

C# List 详解二,C#,c#

9.CopyTo(T[])    

从目标数组的开头开始,将整个 List<T> 复制到兼容的一维数组。

public void CopyTo (T[] array);

参数
array
T[]
一维 Array,它是从 List<T> 复制的元素的目标。 Array 必须具有从零开始的索引。

案例:

using System;
using System.Collections.Generic;

namespace ListTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            List<int> intList = new List<int>() { 2, 5, 33, 6, 23, 45,36,38 };
            int[] list = new int[intList.Count];
            intList.CopyTo(list);
            Console.WriteLine(string.Join("-", list));

            Console.ReadKey();
        }
    }
}

运行:

C# List 详解二,C#,c#

10.CopyTo(T[], Int32)    

从目标数组的指定索引处开始,将整个 List<T> 复制到兼容的一维数组。

public void CopyTo (T[] array, int arrayIndex);

参数
array
T[]
一维 Array,它是从 List<T> 复制的元素的目标。 Array 必须具有从零开始的索引。

arrayIndex
Int32
array 中从零开始的索引,从此处开始复制。

案例:

这里和上一节的用法差不多,只是多了一个 arrayIndex

using System;
using System.Collections.Generic;

namespace ListTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            List<int> intList = new List<int>() { 2, 5, 33, 6, 23, 45,36,38 };
            int[] list = new int[intList.Count + 5];
            intList.CopyTo(list, 5);
            Console.WriteLine(string.Join("-", list));

            Console.ReadKey();
        }
    }
}

运行:

C# List 详解二,C#,c#

第 2 / 7  篇  End文章来源地址https://www.toymoban.com/news/detail-602536.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

领红包