Openstar63
Openstar63

Reputation: 159

How to declare and use arrays in C#

I have a seemingly basic problem with C# arrays. I am a beginner, but this is a really basic problem and it has had me in knots for more than half an hour now. I am using C# 2010 Express.

This code:-

string[] motionCam = new string[8];
motionCam[1] = "Stop";

Reports the error:

Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)

Even basic array examples I copy and paste off of education web sites report this same error and I have no clue why.

Upvotes: 4

Views: 16459

Answers (6)

gdoron
gdoron

Reputation: 150253

While the code you pasted is valid, your problem is something else.
Maybe you declared it in the class level.

motionCam[1] = "Stop"; is an assignment not a declaration, this why the compiler shouts.

See this question of someone who had that the same problem.

Bottom line is You can't have non-declaration statements at the class level.

Upvotes: 5

Eric Lippert
Eric Lippert

Reputation: 660068

The other answers that state that you cannot put that line of code in a class declaration but outside of a method body are correct. I thought it might be interesting to describe why you get the odd error message. The compiler is attempting desperately to try to figure out what you mean by X[Y] = Z;, and it assumes that what you meant was:

X[] F = Z;

That is, that you accidentally put the size of the array in the array declaration -- a very common error amongst C programmers who have recently learned C# -- and have omitted the name of the field.

The compiler therefore gives the most informative error that it can come up with: that you are probably a C programmer who has forgotten that the size of the array goes in the initializer, not the type declaration.

In this case that guess is completely wrong; the error here is that you've accidentally put a statement where a field declaration is expected. But most of the time, that's a reasonable guess.

Upvotes: 11

CodesInChaos
CodesInChaos

Reputation: 108800

This is code that has to go inside a method. If you put it directly in a class, it gives that error.

The first line is a valid declaration, and thus is fine directly inside a class.
The second line is an assignment statement, and not a declaration. Thus it can only appear in a method, not directly in a class.

Put it into a method like this:

static void MyMethod()
{
  string[] motionCam = new string[8];
  motionCam[1] = "Stop";
}

If you put this code directly in a class, the C# compiler interprets motionCam[1] as a type. In C or C++ this would be an array of motionCam elements with size 1. In C# it's invalid.

Upvotes: 1

ispiro
ispiro

Reputation: 27673

You're putting it in a class. (The second line)

Put it in a method.

public partial class Form1 : Form
{
    string[] motionCam = new string[8];
    public Form1()
    {
        InitializeComponent();
        motionCam[1] = "Stop";
    }
}

Upvotes: 5

Chris Shain
Chris Shain

Reputation: 51329

Try putting this line inside a method (like the class constructor maybe):

motionCam[1] = "Stop";

The problem is that you are trying to create the array (which is fine) and then populate it (which is not) inside of the class declaration.

Upvotes: 1

Ðаn
Ðаn

Reputation: 10875

As others have said, there's more to it than just that one line of code. In any case, the specific thing the compiler is looking for (which may not be what you want) is something like:

var motionCam = new string[] { "Zero", "Stop" /*[1]*/, "Two" };

that's the initializing with a 'new' expression part.

Upvotes: 1

Related Questions