List .Add()方法以及C#中的示例
C#List<T>.Add()方法
List<T>.Add()方法用于将对象/元素添加到列表的末尾。
语法:
void List<T>.Add(T item);
参数:它接受单个T类型的项添加到列表中。
返回值:不返回任何内容–返回类型为void
示例
int list declaration:
List<int> a = new List<int>();
adding elements:
a.Add(10);
a.Add(20);
a.Add(30);
a.Add(40);
a.Add(50);
Output:
10 20 30 40 50C#使用List<T>.Add()方法将项目添加到列表的示例
using System;
using System.Text;
using System.Collections.Generic;
namespace Test
{
class Program
{
static void Main(string[] args)
{
//整数列表
List<int> a = new List<int>();
//添加元素
a.Add(10);
a.Add(20);
a.Add(30);
a.Add(40);
a.Add(50);
//打印元素
Console.WriteLine("list elements are...");
foreach(int item in a)
{
Console.Write(item + " ");
}
Console.WriteLine();
//字符串列表
List<string> b = new List<string>();
//添加元素
b.Add("Manju");
b.Add("Amit");
b.Add("Abhi");
b.Add("Radib");
b.Add("Prem");
//打印元素
Console.WriteLine("list elements are...");
foreach (string item in b)
{
Console.Write(item + " ");
}
Console.WriteLine();
//按ENTER退出
Console.ReadLine();
}
}
}输出结果
list elements are... 10 20 30 40 50 list elements are... Manju Amit Abhi Radib Prem
热门推荐
10 广西考试祝福语结婚简短
11 猪年祝福语简短小孩
12 元旦祝福语送长辈简短
13 恭喜二宝祝福语简短
14 祝福语暖心话简短
15 国庆中秋祝福语简短兄弟
16 朋友订婚的祝福语简短
17 送弟弟中秋祝福语简短
18 爱生日祝福语简短独特