Reputation: 81262
I have the following:
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
Upvotes: 213
Views: 288184
Reputation: 2862
GlobalStrings.AddRange(localStrings);
That works.
Documentation: List<T>.AddRange(IEnumerable<T>)
.
Upvotes: 63
Reputation: 144126
GlobalStrings.AddRange(localStrings);
Note: You cannot declare the list object using the interface (IList).
Documentation: List<T>.AddRange(IEnumerable<T>)
.
Upvotes: 344
Reputation: 2757
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);
}
Upvotes: 6
Reputation: 3425
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"});
Upvotes: 2
Reputation: 9067
With Linq
var newList = GlobalStrings.Append(localStrings)
Upvotes: 8