String array in constructor

  • Thread starter Thread starter mattias.k.nyberg
  • Start date Start date
M

mattias.k.nyberg

So Im trying to learn to program with C#. And I have this question
about why the string array won't work in the first class but it does in
the second. To me it looks like they do the exact same thing.

class Test
{
string[] words;
public Test() {
words = { "", "", "" }; // This doesn't work
}
}

class Test2
{
string[] words;
public Test2() {
words = new string[3]; // This works
}
}
 
you must set the length of a static array before using it

sring[] words = new String[3] {"","",""};

or string[] words = new String[3];
words[0] = "";
words[1] = "";
words[2] = "";
 
Hmmm, why does this work then?

class Test
{
string[] words = { "", "", "" };
}


But not this:

class Test
{
string[] words;
public Test() {
words = { "", "", "" };
}
}
you must set the length of a static array before using it

sring[] words = new String[3] {"","",""};

or string[] words = new String[3];
words[0] = "";
words[1] = "";
words[2] = "";

So Im trying to learn to program with C#. And I have this question
about why the string array won't work in the first class but it does in
the second. To me it looks like they do the exact same thing.

class Test
{
string[] words;
public Test() {
words = { "", "", "" }; // This doesn't work
}
}

class Test2
{
string[] words;
public Test2() {
words = new string[3]; // This works
}
}
 
In the first class the line should be the following to compile
correctly.

words = new string[] { "", "", "" };

The second class is not doing the same thing. All it is doing is
creating an array big enough to hold 3 strings, but doesn't actually
initialize the strings. Contrast that with what I have above which
does initialize the strings.

It's been awhile since I've read the specification, but I believe C#
3.0 will accept the syntax in your first class.

Brian
 
Brian said:
It's been awhile since I've read the specification, but I believe C#
3.0 will accept the syntax in your first class.

I just did a cursory check of the specification. I found nothing
indicating this was true.
 
Ah I see now.

This only works if you keep it on the same line.
string[] words = { "", "", "" };

It doesn't if you split it up.
string[] words;
words = { "", "", "" }; // Error

Then I need to use:
string[] words;
words = new string[] { "", "", "" };


Thanks for the help :)

Brian said:
In the first class the line should be the following to compile
correctly.

words = new string[] { "", "", "" };

The second class is not doing the same thing. All it is doing is
creating an array big enough to hold 3 strings, but doesn't actually
initialize the strings. Contrast that with what I have above which
does initialize the strings.

It's been awhile since I've read the specification, but I believe C#
3.0 will accept the syntax in your first class.

Brian


So Im trying to learn to program with C#. And I have this question
about why the string array won't work in the first class but it does in
the second. To me it looks like they do the exact same thing.

class Test
{
string[] words;
public Test() {
words = { "", "", "" }; // This doesn't work
}
}

class Test2
{
string[] words;
public Test2() {
words = new string[3]; // This works
}
}
 
Hmmm, why does this work then?

class Test
{
string[] words = { "", "", "" };
}

You can only initialize arrays this way at the time they are declared
string[] words = { "", "", "" };
is shorthand for
string[] words = new string[]{"","",""};
but only in the declaration

I don't know the reason why it is limited to the declaration. On the
surface it seems like a reasonable thing to want to do, but perhaps the
devil is in the details.

Hope this helps
Bill


But not this:

class Test
{
string[] words;
public Test() {
words = { "", "", "" };
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top