1

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?

9
  • Have you debugged to make sure the list definitely contains more than a single item? Commented Jan 22, 2013 at 1:36
  • You're doing it right something else is wrong Commented Jan 22, 2013 at 1:37
  • Code looks fine, can you post a more complete example that demonstrates your problem? Commented Jan 22, 2013 at 1:37
  • what does the list contain? Can you show us what list looks like Commented Jan 22, 2013 at 1:37
  • are you looking to split the characters?if so you can just do text.ToArray() Commented Jan 22, 2013 at 1:41

4 Answers 4

5

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

1

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(',');

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.