Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ochreous · Aug 26, 2013 at 03:47 AM · c#listadd

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image getyour411 · Aug 26, 2013 at 04:02 AM 1
Share

Look at AddRange and InsertRange here, see if that's what you want

avatar image Ochreous · Aug 26, 2013 at 04:20 AM 0
Share

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.

avatar image getyour411 · Aug 26, 2013 at 04:38 AM 0
Share

That's not how I read it, I think the first list in both examples was just to show the 'normal' way

5 Replies

· Add your reply
  • Sort: 
avatar image
11
Best Answer

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]);
          }
     }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
11

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
3

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image darshie1976 · Feb 22, 2016 at 03:30 AM 1
Share

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?

avatar image ijustneedananswer darshie1976 · May 08, 2016 at 10:07 AM 0
Share

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

avatar image
-1

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");

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tanoshimi · May 06, 2016 at 09:09 PM 0
Share

That adds one item to the list, called "text1text2text3text4"....

avatar image
0

Answer by corapeca · Oct 24, 2020 at 06:58 PM

 someStringList.AddRange(new List<String>() {"text1","text2","text3","text4"});
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges