- Home /
Issues outputting a list of strings
I have a list of strings:
//TODO: Put these lists in a separate method
public static List<string> task1GetNames = new List<string>(new[]
{
"Gru",
"Bob",
"Peter
});
Then I have another list that I use to store the values of the first list in it
public static List<string> selection = new List<string>();
In another class I am saving the contents of the first list into the new list
MyClass.selection.Add(MyClass.task1GetNames[MyClass.randomIndex_task1]);
After that I place the contents of the list into this string array:
string[] myNameList = { MyClass.selection.ToString(),
MyClass.selection.ToString(),
MyClass.selection.ToString()
};
But the output I see is System.GenericList1..... on all three ouputs.
And finally I pass the strings into a selection grid so that they are printed onto the screen:
drawSelectionGrid(selGrid_NameOptions, myNameList, ref nameIndex, 3);
The issue I'm having is that the output is coming out wrong, it is like it is giving me some systems application debug information and not the names I listed above in the string list. What can I do in order to get all names to output correctly?
Answer by Key_Less · Jan 02, 2014 at 09:13 PM
That's because a generic container, such as a List, does not override the ToString() method so converting it to a string will not return the values stored inside it, but instead returns the fully qualified name of it's type which is System.GenericList.
Try using the ToArray() method to copy your data over to the string array. For example:
string[] myNameList = MyClass.selection.ToArray();
Also, I'm not sure if this was intentional or not, but just in case, I thought I'd mention that your line of code...
MyClass.selection.Add(MyClass.task1GetNames[MyClass.randomIndex_task1]);
...will only add one name from the task1GetNames list into your selection list. If you want to add the entire contents of the list into MyClass.selection you could use AddRange():
MyClass.selection.AddRange(MyClass.task1GetNames);
Your answer
![](https://koobas.hobune.stream/wayback/20220613125402im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Printing an ordered list 1 Answer
C# Adding Multiple Elements to a List on One Line 5 Answers
C# how to create a Descending GUI List 1 Answer
Output a string list 1 Answer