Home > other >  C# Why cant I add more items to a list of strings? I can add a singular item
C# Why cant I add more items to a list of strings? I can add a singular item

Time:08-09

private List<string> _listOfWords = new List<string>();

 _listOfWords.Add("tell", "hey"); 

//Why does not this work? I can add one item but not multiple

CodePudding user response:

List<T>.Add(T) only accepts a single parameter.

So multiple items can be added by call the method multiple times like

list.Add("item1");
list.Add("item2");

or you can use another method like List<T>.AddRange(IEnumerable<T>) to add multiple items in a single call

list.AddRange(new string[] { "item1", "item", ... });

CodePudding user response:

Are you looking perhaps for AddRange?

_listOfWords.AddRange(new[] { "tell", "hey" });
  •  Tags:  
  • c#
  • Related