Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

When using the .ToArray() function is it necessary to implicitly define the size of the array in order to hold of the characters that were in the list you're converting?

String [] array = List.ToArray();

I tried doing this because I needed to use the .GetValue() ability of the array, however, the array is maintaining a size of 1 and not holding the material from the list. Am I trying to use the .ToArray() incorrectly?

colAndDelimiter = new List<string>();
colAndDelimiter.Add(text);

String [] cd = colAndDelimiter.ToArray();

This is all of the code I have that effects the array. When I Console.WriteLine() the list it gives me the entirety of the text. I may be confused about how list works. Is it storing everything as a single item, and that's why the array only shows one place?

share|improve this question
Have you debugged to make sure the list definitely contains more than a single item? – Simon Whitehead Jan 22 at 1:36
What is List exactly? – msmith0957 Jan 22 at 1:36
You're doing it right something else is wrong – Eli Algranti Jan 22 at 1:37
Code looks fine, can you post a more complete example that demonstrates your problem? – bmm6o Jan 22 at 1:37
what does the list contain? Can you show us what list looks like – Dan Hunex Jan 22 at 1:37
show 6 more commentsadd comment (requires an account with 50 reputation)

4 Answers

It should work fine, but try the var operator to be sure.

var array = List.ToArray();

Is there a reason to use Array.GetValue instead of the built in functions of the List<T> itself EG:

 string value = List.ElementAt(1);
 var values = List.GetRange(0, 5);
share|improve this answer
add comment (requires an account with 50 reputation)

As long as your List object is some IEnumerable (or similar) that has items in it, this should indeed convert your List to an Array.

Note that once you have created this array, adding elements to List won't also add it to the Array

share|improve this answer
add comment (requires an account with 50 reputation)

You don't need to convert it to an array to get specific characters out.

Just use text[index] to get at the needed character.

If you really need it as an array, use String.ToCharArray() - you want an array of char not an array of string.

Edit:

Is it storing everything as a single item, and that's why the array only shows one place?

Yes, yes it is. You're making a list of strings which contains one string: the entire contents of text - what it seems that you want is to split it up letter by letter, which is what the above methods will achieve.

share|improve this answer
add comment (requires an account with 50 reputation)

What you are doing is fine.. but lets say that you are not sure of the size of the string[] cd then you can do something like the following

var colAndDelimiter = new List<string>();
colAndDelimiter.Add(text);
String[] cd = { }; 
cd =  colAndDelimiter.ToArray();

find the position of the data in cd string value = cd[0];

Update: If you want to do this based on values being stored in a single line then you can do this without having to declare the cd variable as string[] cd;

var colAndDelimiter = new List<string>();
colAndDelimiter.Add("Hello, World, Week, Tuesday");
var cd = colAndDelimiter[0].Split(',');
share|improve this answer
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.