简体   繁体   English

如何在C#中声明和使用数组

[英]How to declare and use arrays in C#

I have a seemingly basic problem with C# arrays. 我对C#数组有一个看似基本的问题。 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. 我正在使用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) 无法在变量声明中指定数组大小(尝试使用'new'表达式初始化)

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; 编译器正在拼命试图弄清楚你的意思是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. 也就是说,您不小心将数组的大小放在数组声明中 - 这是最近学习过C#的C程序员中一个非常常见的错误 - 并且省略了该字段的名称。

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. 因此,编译器提供了它可以提供的最丰富的错误:您可能是一个C程序员,他忘记了数组的大小是在初始化程序中,而不是类型声明。

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. 这是initializing with a 'new' expression部分initializing with a 'new' expression

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. 如果将此代码直接放在类中,C#编译器会将motionCam[1]解释为类型。 In C or C++ this would be an array of motionCam elements with size 1 . 在C或C ++中,这将是一个大小为1motionCam元素数组。 In C# it's invalid. 在C#中它无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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