- Home /
C# Adding Multiple Elements to a List on One Line
Hi everyone, is there a function I can use to add multiple elements to a list on one line? I want to compress something like this.
someStringList.Add("text1");
someStringList.Add("text2");
someStringList.Add("text3");
someStringList.Add("text4");
Into to this.
someStringList.Add("text1","text2","text3","text4");
I've tried the following above. But I get an error saying Add doesn't take four arguments.
Look at AddRange and InsertRange here, see if that's what you want
Neither of those are what I'm looking for. They require two lists to begin with and What I'm looking to do is add multiple elements to just one list.
That's not how I read it, I think the first list in both examples was just to show the 'normal' way
Answer by robertbu · Aug 26, 2013 at 04:58 AM
You can write your own function with a variable number of parameter, or you can use AddRange() as @getyour411 suggests:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Bug25 : MonoBehaviour {
List<string> someStringList = new List<string>();
void Start() {
// Calling method with variable number of parameters
AddToList ("One", "Two", "Three");
for (int i = 0; i < someStringList.Count; i++)
Debug.Log (someStringList[i]);
// Two step AddRange()
string[] input = {"four", "five", "six"};
someStringList.AddRange (input);
Debug.Log ("-----");
for (int i = 0; i < someStringList.Count; i++)
Debug.Log (someStringList[i]);
// One step AddRange()
someStringList.AddRange (new string[]{"seven","eight", "nine"});
Debug.Log ("-----");
for (int i = 0; i < someStringList.Count; i++)
Debug.Log (someStringList[i]);
}
// Function with a variable number of parameters
void AddToList(params string[] list) {
for ( int i = 0 ; i < list.Length ; i++ ) {
someStringList.Add(list[i]);
}
}
}
Answer by ArkaneX · Aug 26, 2013 at 07:38 AM
@robertbu's answer is good enough, but I would implement it using extension method. Firstly you have to create a static class, let's name it ListExtensions, with a method to add elements:
using System.Collections.Generic;
public static class ListExtenstions
{
public static void AddMany<T>(this List<T> list, params T[] elements)
{
list.AddRange(elements);
}
}
and then you can simply use:
var list = new List<string> { "x", "y" };
list.AddMany("a", "z");
This way you can use it from any class and for List of any type of elements.
Answer by ijustneedananswer · Mar 26, 2015 at 07:41 AM
The link that @getyour411 provided shows the InsertRange() method which will totally do what you were asking, but with some slight tweaking.
List<string> stringList = new List<string> ();
stringList.InsertRange (stringList.Count, new string[] {"one", "two", "three"});
The first argument for InsertRange is the index (i.e. where you want to start inserting the range). Using stringList.Count as the index ensures that you will always add it to the end of the list, even if the list is empty.
The second argument is a collection of the specified type (string in this case) to be inserted. I went with an array because it takes up slightly less horizontal space in your code, but you could also use a list. The last line of code would then look like this:
stringList.InsertRange (stringList.Count, new List<string> {"one", "two", "three"});
Sorry for responding to an old thread, but other visitors from the future like myself might be interested in this.
Aren't you adding a string, which is the sum of all the strings in the array? So when you print the value at index 0 for example, you won't get "one", but "{one, two, three}". Or is C# smart enough to understand that for each element in the array, it has to increment the index and append the next element in the array?
You're second hunch was the correct one: C# is smart enough to figure it out. Check this link out for a detailed explanation: http://www.dotnetperls.com/insertrange
Answer by Crocodile0 · May 06, 2016 at 08:46 PM
The following code will work, its just the way to concatenate multiple strings into one:
someStringList.Add("text1" +"text2" +"text3" + "text4");
That adds one item to the list, called "text1text2text3text4"....
Answer by corapeca · Oct 24, 2020 at 06:58 PM
someStringList.AddRange(new List<String>() {"text1","text2","text3","text4"});
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
C# Displaying List Elements in Multiples 1 Answer
C# Randomly Adding Elements from stringListA to stringListB 1 Answer
C# Dividing Gameobject List by Half 1 Answer
C# Displaying a List in Series 2 Answers