hiiiiii
i have some doubt plz clear me.
. Q. when adding a collection to the
array list, we are using AddRange, instead of Add. why it is? what is the need of AddRange?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Posted Sep 4, 2013, 7:33 AM
VulpesPosted Sep 4, 2013, 5:33 AM
Sanjeeb LenkaPosted Sep 3, 2013, 2:44 AM
AddRange adds an entire collection of elements. It can replace tedious foreach-loops that repeatedly call Add on List.
Add will add one item at a time.
ex:
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List
a.Add(1);
a.Add(2);
a.Add(5);
a.Add(6);
// Contains:
// 1
// 2
// 5
// 6
int[] b = new int[3];
b[0] = 7;
b[1] = 6;
b[2] = 7;
a.AddRange(b);
// Contains:
// 1
// 2
// 5
// 6
// 7 [added]
// 6 [added]
// 7 [added]
foreach (int i in a)
{
Console.WriteLine(i);
}
}
}
Output
1
2
5
6
7
6
7