简体   繁体   中英

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.

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.

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.

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";
    }
}

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM