C#中ArrayList的Add和AddRange有什么区别?

12

有谁能告诉我何时使用 Add()AddRange() 两个方法呢?它们都属于 ArrayList 类。


4
Add 添加一个单一的值,例如 Add(1);而 AddRange 则添加一个 IEnumerable<T> 类型的多个值,例如 AddRange(new int[] {1, 2, 3}) - Dmitry Bychenko
一个简单的搜索将带您找到许多好的文章,包括MSDN文档。http://geekswithblogs.net/sdorman/archive/2007/09/21/Add-vs.-AddRange.aspx - Rahul Singh
7
你为什么不直接阅读他们的文档呢?顺便说一下,现在是2015年。如果你还在使用ArrayList,就不要用了。那是在C#没有泛型的旧时代。你应该改用List<T> - Soner Gönül
请查看文档:https://msdn.microsoft.com/de-de/library/system.collections.arraylist(v=vs.110).aspx - Joehl
你应该从谷歌上找到答案了。 - Mairaj Ahmad
1
你有阅读方法的文档吗? - il_raffa
6个回答

14
如果您想一次性添加大量值,请使用 AddRange。 如果您只是偶尔添加一个单独的值或者添加的值较少,请使用 Add

12
添加和添加范围的区别 Add:它用于逐个将项目添加到列表中。 AddRange:它用于将大量列表项添加到另一个列表中。
List<string>list1=new List<string>();//using Add
List<string>list2=new List<string>();//using AddRange
list1.Add("Malathi");
list1.Add("Sandhiya");
list1.Add("Ramya");
list1.Add("Mithra");
list1.Add("Dharshini");

list2.AddRange(list1);
list1的输出包含:
  • Malathi, Sandhiya, Ramya, Mithra, Dharshini
list2的输出包含:
  • Malathi, Sandhiya, Ramya, Mithra, Dharshini

1
C# List类表示C#中的一种类型的集合。List.Add()、List.AddRange()、List.Insert()和List.InsertRange()方法用于向List添加和插入项。 AddRange - AddRange添加整个元素集合。它可以替换繁琐的foreach循环,这些循环不断调用List上的Add。
public virtual void AddRange (System.Collections.ICollection c);

Add - Add方法将一个对象添加到List的末尾。
public virtual int Add (object value);

示例:现在设置一个元素数组以添加到列表中。

// array of 4 elements
int[] arr = new int[4];
arr[0] = 500;
arr[1] = 600;
arr[2] = 700;
arr[3] = 800;

使用AddRange()方法将整个元素集合添加到列表中 -
List<int> list = new List<int>();
list.AddRange(arr);

但是如果您想使用List.Add()方法,

List<int> list = new List<int>();
list.Add(100);
list.Add(200);
list.Add(300);
list.Add(400);

有关详细信息,您可以查看将项目插入到C#列表中


0
如果您想在列表中添加单个变量,可以使用Add()方法。 但是,如果您想要添加列表或多个变量到列表中,则可以使用AddRange()方法。
var t = (from t1 intable1
         join t2 in table2 on t1.t1id equals t2.t2id
         select new ABCViewModel
         {
             FirstName = t1.firstname,
             LastName = t1.Lastname
         })
        .where(t2.age>35)
        .ToList();
var s = (from t1 intable1
         join t2 in table2 on t1.t1id equals t2.t2id
         select new ABCViewModel
         {
             FirstName = t1.firstname,
             LastName = t1.Lastname
         })
        .where(t2.age < 35)
        .ToList();
t.AddRange(s);
return t;

它将把List s的结果与List t的结果一起添加到List t中。

0

Add()AddRange()方法的区别非常明显。

Add()用于向列表中添加一个元素。

AddRange()用于一次性添加一系列元素(多个元素)到列表中。

注意:多个元素可以是另一个完整的ArrayHashTableSortedListArrayListBitArrayQueue或者Stack


-3
namespace ConsoleApplication2
{

class Program
    {

  static void Main(string[] args)
        {

  //create the first arraylist

            ArrayList arraylist1 = new ArrayList();

            arraylist1.Add(5);

            arraylist1.Add(7);

            //create the second arraylist

            ArrayList arraylist2 = new ArrayList();

            arraylist2.Add("Five");//add the single value at time to the arraylist

            arraylist2.Add("Seven");//add the single value at time to the arraylist

            //perform AddRange method

            arraylist1.AddRange(arraylist2);//adding the arraylist as bulk in another arraylist

            // Display the values.

            foreach (object i in arraylist1)//iterating the arraylist1 value to object
            {
                Console.WriteLine(i);
            }
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接