data.add方法c#
C#List <T> .Add()方法 (C# List<T>.Add() Method)
List<T>.Add() method is used to add the object/element at the end of the list.
List <T> .Add()方法用于将对象/元素添加到列表的末尾。
Syntax:
句法:
void List<T>.Add(T item);
Parameter: It accepts single an item 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>();
adding elements:
a.Add(10);
a.Add(20);
a.Add(30);
a.Add(40);
a.Add(50);
Output:
10 20 30 40 50
使用List <T> .Add()方法将项目添加到列表的C#示例 (C# Example to add items to the list using List<T>.Add() 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>();
//adding elements
a.Add(10);
a.Add(20);
a.Add(30);
a.Add(40);
a.Add(50);
//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>();
//adding elements
b.Add("Manju");
b.Add("Amit");
b.Add("Abhi");
b.Add("Radib");
b.Add("Prem");
//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 30 40 50
list elements are...
Manju Amit Abhi Radib Prem
翻译自: https://www.includehelp.com/dot-net/list-t-add-method-with-example-in-c-sharp.aspx
data.add方法c#