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.
1 Like
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*);*
-
// Two step AddRange()*
-
string[] input = {"four", "five", "six"};*
-
someStringList.AddRange (input);*
-
Debug.Log ("-----");*
-
for (int i = 0; i < someStringList.Count; i++)*
_ Debug.Log (someStringList*);*_
* // One step AddRange()*
* someStringList.AddRange (new string[]{“seven”,“eight”, “nine”});*
* Debug.Log (“-----”);*
* for (int i = 0; i < someStringList.Count; i++)*
_ Debug.Log (someStringList*);
}
// Function with a variable number of parameters*
* void AddToList(params string list) {
for ( int i = 0 ; i < list.Length ; i++ ) {
someStringList.Add(list);
}
}
}*_
@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.
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.
someStringList.AddRange(new List() {“text1”,“text2”,“text3”,“text4”});
The following code will work, its just the way to concatenate multiple strings into one:
someStringList.Add(“text1” +“text2” +“text3” + “text4”);