c#addrange_清单 .AddRange()方法与C#中的示例

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值