207

I have the following:

  1. A main List called GlobalStrings
  2. Another List called localStrings

In a loop for example:

List<string> GlobalStrings = new List<string>(); 
List<string> localStrings = new List<string>();
for(x=1;x<10;x++)
{
    localStrings.Add("some value"); 
    localStrings.Add("some value");
}
// Want to append localStrings to GlobalStrings as easily as possible
arnavakhoury
  • 125
  • 1
  • 8
JL.
  • 78,954
  • 126
  • 311
  • 459
  • 4
    stack over flow always rocks...nice question... – Sangram Nandkhile Dec 28 '10 at 08:00
  • 2
    You are doing it wrong, `localStrings = new List;` should be placed before the `for` loop – Wassim AZIRAR Dec 07 '14 at 19:59
  • 1
    Wassim, I think that's why he differentiates between local and global; localStrings is local to the for-loop scope, GlobalStrings is in the global scope. – Dagrooms Jun 12 '15 at 16:15
  • This is a poor example (that invites totally wrongheaded comments like Wassim's), since obviously you could just `Add` to `GlobalStrings` instead of to `localStrings`. And FWIW you loop only runs 9 times. Better would be `for (int x = 0; x < 10; ++x) {var localStrings = GetAListOfStrings(); /* append those to GlobalStrings */}` – Jim Balter Nov 10 '18 at 20:06
  • 11 years later... – Chris Catignani Oct 12 '20 at 19:01

6 Answers6

330
GlobalStrings.AddRange(localStrings);

Note: You cannot declare the list object using the interface (IList).
Documentation: List<T>.AddRange(IEnumerable<T>).

Cœur
  • 37,241
  • 25
  • 195
  • 267
Lee
  • 142,018
  • 20
  • 234
  • 287
60
GlobalStrings.AddRange(localStrings);

That works.

Documentation: List<T>.AddRange(IEnumerable<T>).

Cœur
  • 37,241
  • 25
  • 195
  • 267
Jimmeh
  • 2,834
  • 1
  • 22
  • 21
22

Try AddRange-method:

GlobalStrings.AddRange(localStrings);
martin
  • 2,957
  • 3
  • 25
  • 46
8

With Linq

var newList = GlobalStrings.Append(localStrings)
Blake Pettersson
  • 8,927
  • 3
  • 27
  • 36
  • 3
    Append doesn't seem to exist? Do you have a link to MSDN doc for it? – Pod May 31 '16 at 21:41
  • 5
    Perhaps you meant Concat ... though that would mean a lot of copying, and is a good illustration of how *not* to use LINQ. – Jim Balter Nov 10 '18 at 20:07
6

Here is my example:

    private List<int> m_machinePorts = new List<int>();

    public List<int> machinePorts
    {
        get { return m_machinePorts; }
    }

    Init()
    {
        // Custom function to get available ethernet ports
        List<int> localEnetPorts = _Globals.GetAvailableEthernetPorts();

        // Custome function to get available serial ports
        List<int> localPorts = _Globals.GetAvailableSerialPorts();

        // Build Available port list 
        m_machinePorts.AddRange(localEnetPorts);
        m_machinePorts.AddRange(localPorts);
     }
Jim Lahman
  • 2,691
  • 2
  • 25
  • 21
2

if you want to get "terse" :)

List<string>GlobalStrings = new List<string>(); 

for(int x=1; x<10; x++) GlobalStrings.AddRange(new List<string> { "some value", "another value"});
BillW
  • 3,415
  • 4
  • 27
  • 46