📌  相关文章
📜  C#|将指定集合的元素添加到列表的末尾

📅  最后修改于: 2021-05-29 21:24:58             🧑  作者: Mango

List .AddRange(IEnumerable )方法用于将指定集合的元素添加到List 的末尾。

列表的属性:

  • 它与数组不同。列表可以动态调整大小,但数组则不能。
  • 列表类可以接受null作为引用类型的有效值,并且还允许重复的元素。
  • 如果Count等于Capacity,则List的容量会通过重新分配内部数组自动增加。在添加新元素之前,现有元素将被复制到新数组。

句法:

public void AddRange (System.Collections.Generic.IEnumerable collection);

范围:

异常:如果集合为null,则此方法将提供ArgumentNullException。

注意:集合本身不能为null,但是如果类型T是引用类型,则它可以包含为null的元素。集合中元素的顺序始终保留在List 中。

下面的程序说明了上面讨论的方法的使用:

范例1:

// C# program to illustrate the
// List.AddRange Method
using System;
using System.Collections.Generic;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating a List of Strings
        List firstlist = new List();
  
        // adding elements in firstlist
        firstlist.Add("Geeks");
        firstlist.Add("GFG");
        firstlist.Add("C#");
        firstlist.Add("Tutorials");
  
        Console.WriteLine("Before AddRange Method");
        Console.WriteLine();
  
        // displaying the item of List
        foreach(String str in firstlist)
        {
            Console.WriteLine(str);
        }
  
        Console.WriteLine("\nAfter AddRange Method\n");
  
        // Here we are using AddRange method
        // Which adds the elements of firstlist
        // Collection in firstlist again i.e.
        // we have copied the whole firstlist
        // in it
        firstlist.AddRange(firstlist);
  
        // displaying the item of List
        foreach(String str in firstlist)
        {
            Console.WriteLine(str);
        }
    }
}

输出:

Before AddRange Method

Geeks
GFG
C#
Tutorials

After AddRange Method

Geeks
GFG
C#
Tutorials
Geeks
GFG
C#
Tutorials

范例2:

// C# program to illustrate the
// List.AddRange Method
using System;
using System.Collections.Generic;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating a List of Strings
        List firstlist = new List();
  
        // adding elements in firstlist
        firstlist.Add("Geeks");
        firstlist.Add("GFG");
        firstlist.Add("C#");
        firstlist.Add("Tutorials");
  
        Console.WriteLine("Before AddRange Method");
        Console.WriteLine();
  
        // displaying the item of List
        foreach(String str in firstlist)
        {
            Console.WriteLine(str);
        }
  
        Console.WriteLine("\nAfter AddRange Method\n");
  
        // taking array of String
        string[] str_add = { "Collections",
                             "Generic",
                             "List" };
  
        // here we are adding the elements
        // of the str_add to the end of
        // the List.
        firstlist.AddRange(str_add);
  
        // displaying the item of List
        foreach(String str in firstlist)
        {
            Console.WriteLine(str);
        }
    }
}

输出:

Before AddRange Method

Geeks
GFG
C#
Tutorials

After AddRange Method

Geeks
GFG
C#
Tutorials
Collections
Generic
List

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.generic.list-1.addrange?view=netframework-4.7.2