C#中的List .AddRange()方法以及示例
C#List<T>.AddRange()方法
List<T>.AddRange()方法用于在列表的末尾添加指定集合的对象/元素。
语法:
void List<T>.AddAddRange(IEnumerable<T> collection);
参数:它接受要添加到列表中的T类型的元素(如数组)的集合。
返回值:不返回任何内容–返回类型为void
示例
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 400C#使用List<T>.AddRange()方法将项目添加到列表的示例
using System;
using System.Text;
using System.Collections.Generic;
namespace Test
{
class Program
{
static void Main(string[] args)
{
//整数列表
List<int> a = new List<int>();
//要在列表中添加的int数组
int[] int_arr = { 100, 200, 300, 400 };
//添加元素
a.Add(10);
a.Add(20);
//增加范围
a.AddRange(int_arr);
//打印元素
Console.WriteLine("list elements are...");
foreach(int item in a)
{
Console.Write(item + " ");
}
Console.WriteLine();
//字符串列表
List<string> b = new List<string>();
//要添加到列表中的字符串数组
string[] str_arr = { "Abhi", "Radib", "Prem" };
//添加元素
b.Add("Manju");
b.Add("Amit");
//增加范围
b.AddRange(str_arr);
//打印元素
Console.WriteLine("list elements are...");
foreach (string item in b)
{
Console.Write(item + " ");
}
Console.WriteLine();
//按ENTER退出
Console.ReadLine();
}
}
}输出结果
list elements are... 10 20 100 200 300 400 list elements are... Manju Amit Abhi Radib Prem
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短