lilli
lilli

Reputation: 55

String[] does not contain the definition for ToArray() and no extension method

I am trying to store selected checkboxes values using StringBuilder and foreach loop, then convert it into the string array.

Here is my code

public ActionResult Home(CheckList obj)
    {
        StringBuilder sb = new StringBuilder();    
        foreach (var item in obj.Checkboxes)
        {
            if (item.IsChecked)
                sb.Append(item.Value).ToString();

        }

       string[] col = sb.ToString().Split(' ').ToArray();

 ..
}

But I got an error on this line of no definition of ToArray() to String[]

string[] col = sb.ToString().Split(' ').ToArray(); 

Kindly help me how can I change my string into a string array.

Upvotes: 0

Views: 2489

Answers (3)

Croos Nilukshan
Croos Nilukshan

Reputation: 154

str.Split return an array of string. No need to convert it again to Array.

string str = sb.ToString();
string[] col = null;
int count = 0;
char[] splitchar = { ' ' };
col = str.Split(splitchar);

Upvotes: 1

Just Shadow
Just Shadow

Reputation: 11881

ToArray() method is included in Linq, so simply using linq should work:

using System.Linq;

Upvotes: 3

YonahW
YonahW

Reputation: 16520

As you can see here in the documentation, Split already returns a string array.

Upvotes: 4

Related Questions