c#addrange
C#List <T> .AddRange()方法 (C# List<T>.AddRange() Method)
List<T>.AddRange() method is used to add the objects/elements of a specified collection at the end of the list.
List <T> .AddRange()方法用于将指定集合的对象/元素添加到列表的末尾。
Syntax:
句法:
void List<T>.AddAddRange(IEnumerable<T> collection);
Parameter: It accepts a collection of elements (like, arrays) of T type to add in the List.
参数:它接受要添加到列表中的T类型的元素(如数组)的集合。
Return value: It returns nothing – it's return type is void
返回值:不返回任何内容–返回类型为void
Example:
例:
int list declaration:
List<int> a = new List<int>();
int array to be added to the list:
int[] int_arr = { 100, 200, 300, 400 };
adding elements:
a.AddRange(int_arr);
Output:
100 200 300 400
使用List <T> .AddRange()方法将项目添加到列表的C#示例 (C# Example to add items to the list using List<T>.AddRange() Method)
using System;
using System.Text;
using System.Collections.Generic;
namespace Test
{
class Program
{
static void Main(string[] args)
{
//integer list
List<int> a = new List<int>();
//int array to add in the list
int[] int_arr = { 100, 200, 300, 400 };
//adding elements
a.Add(10);
a.Add(20);
//adding range
a.AddRange(int_arr);
//printing elements
Console.WriteLine("list elements are...");
foreach(int item in a)
{
Console.Write(item + " ");
}
Console.WriteLine();
//string list
List<string> b = new List<string>();
//string array to add in the list
string[] str_arr = { "Abhi", "Radib", "Prem" };
//adding elements
b.Add("Manju");
b.Add("Amit");
//adding range
b.AddRange(str_arr);
//printing elements
Console.WriteLine("list elements are...");
foreach (string item in b)
{
Console.Write(item + " ");
}
Console.WriteLine();
//hit ENTER to exit
Console.ReadLine();
}
}
}
Output
输出量
list elements are...
10 20 100 200 300 400
list elements are...
Manju Amit Abhi Radib Prem
翻译自: https://www.includehelp.com/dot-net/list-t-addrange-method-with-example-in-c-sharp.aspx
c#addrange