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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Sep 2, 2013, 10:44 PM EDT
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
VulpesRank #55 · 29.2K reputationPosted Sep 4, 2013, 1:33 AM EDT
Posted Sep 4, 2013, 3:33 AM EDT