How to declare and use arrays in C #

I have a seemingly basic problem with C # arrays. I am new, but this is really the main problem, and now I get it in nodes more than in half an hour. I am using C # 2010 Express.

This code: -

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

Report an error:

The size of the array cannot be specified in the variable declaration (try initializing with the "new" expression)

Even the examples of the underlying array that I copy and attach to educational websites report the same error, and I don't know why.

+4
source share
6 answers

As long as the code you entered is valid, your problem is something else.
You may have declared it at the class level.

motionCam[1] = "Stop"; is an assignment, not an declaration, so the compiler screams.

See this question about who had the same problem.

Bottom line You cannot have ad statements without an ad at the class level.

+5
source

Other answers stating that you cannot put this line of code in a class declaration, but outside the method body, are correct. I thought it would be interesting to describe why you get the error message. The compiler is trying desperately to figure out what you mean by X[Y] = Z; , and suggests that you mean:

 X[] F = Z; 

That is, you accidentally placed the size of the array in an array declaration - a very common mistake among C programmers who recently learned C # and omitted the field name.

Therefore, the compiler gives the most informative error that it may encounter: you are probably a C programmer who forgot that the size of the array goes in the initializer, and not in the type declaration.

In this case, this assumption is completely false; the error here is that you accidentally put in an operator where a field declaration is expected. But most of the time this is a reasonable assumption.

+11
source

You put him in class. (Second line)

Put it in a method.

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

As others have said, there is more than just one line of code. In any case, the specific thing the compiler is looking for (which might not be what you want ) looks something like this:

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

that part is initializing with a 'new' expression .

+1
source

Try putting this line inside a method (for example, a class constructor):

 motionCam[1] = "Stop"; 

The problem is that you are trying to create an array (this is normal) and then populate it (which is not the case) inside the class declaration.

+1
source

This is the code that must pass inside the method. If you put it directly in the class, this will result in an error.

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

Put it in 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 will be an array of motionCam elements with a size of 1 . In C #, this is not valid.

+1
source

Source: https://habr.com/ru/post/1392572/


All Articles