Julian Moreno
Julian Moreno

Reputation: 1094

c# place string into an array

This could be very easy, but how can I place or convert a string into an array?

The code that I have, is the following:

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string one;
        string[] two;

        one = "Juan";
        two = {one}; // here is the error

        HttpContext.Current.Response.Write(two);
    }
}

And the error is the following: Compiler Error Message: CS0029: Cannot implicitly convert type 'string' to 'string[]'

Thanks for you help!

Upvotes: 0

Views: 122

Answers (4)

Tim S.
Tim S.

Reputation: 56536

That syntax ({one}) is only valid if you declare the array variable in the same line. So, this works:

string one;

one = "Juan";
string[] two = {one};

A more common way to initialize an array, which works in more places, is to use the new keyword, and optionally have the type be inferred, e.g.

string one;
string[] two;

one = "Juan";
// type is inferrable, since the compiler knows one is a string
two = new[] {one};
// or, explicitly specify the type
two = new string[] {one};

I usually declare and initialize on the same line, and use var to infer the type, so I'd probably write:

var one = "Juan";
var two = new[] { one };

Upvotes: 0

Habib
Habib

Reputation: 223247

Replace this:

two = {one}; // here is the error

With

two = new[] { one }; 

OR

two = new string[] { one };

The reason you are getting the error is clear from the error message.

See: Object and Collection Initializers (C# Programming Guide)

Later when you are doing Response.Write, you will get System.String[] as output, since two is an array. I guess you need all array elements separated by some delimiter. You can try:

HttpContext.Current.Response.Write(string.Join(",", two));

Which will produce all the elements in the array separated by comma

Upvotes: 5

evanmcdonnal
evanmcdonnal

Reputation: 48086

You're using the static initializer syntax to try and add an item to your array. That doesn't work. You can use similar syntax to allocate a new array with the value one - two = new string[] { one }; - or you can allocate the array then add elements through assignment like;

    string[] two = new string[10];
    two[0] = one; // assign value one to index 0

If you do it like this you have to do some bounds checking for example the following will throw an IndexOutOfRangeException at runtime;

    string[] two = new string[10];
    int x = 12;
    two[x] = one; // index out of range, must ensure x < two.Length before trying to assign to two[x]

Upvotes: 0

D Stanley
D Stanley

Reputation: 152521

It looks like you're trying to use initialization syntax for an assignment. This should work:

two = new string[] {one};

or just

two = new [] {one};

since the compiler will infer that you want a string[]

I think you'll also be surprised what Response.Write(two); produces...

Upvotes: 2

Related Questions