为什么 `new int[x]{}` 无效?

发布于 2024-09-02 09:30:55 字数 463 浏览 6 评论 0原文

在 MonoDevelop 中,我有以下可以编译的代码:

int[] row = new int[indices.Count]{};

但是,在运行时,我得到:

Matrix.cs(53,53):错误 CS0150:A 预期值恒定 (CS0150) (测试矩阵)

我知道这个错误意味着什么,并迫使我调整数组大小:

int[] row = new int[indices.Count]{};
Array.Resize(ref row, rowWidth);

这是我必须处理的事情吗,因为我在 Linux 上使用 MonoDevelop?我确信在 .Net 3.5 下我能够使用包含数组宽度的变量来初始化数组。谁能确认这是孤立的吗?如果是这样,我可以向 bugzilla 报告该错误。

In MonoDevelop I have the following code which compiles:

int[] row = new int[indices.Count]{};

However, at run-time, I get:

Matrix.cs(53,53): Error CS0150: A
constant value is expected (CS0150)
(testMatrix)

I know what this error means and forces me to then resize the array:

int[] row = new int[indices.Count]{};
Array.Resize(ref row, rowWidth);

Is this something I just have to deal with because I am using MonoDevelop on Linux? I was certain that under .Net 3.5 I was able to initialize an array with a variable containing the width of the array. Can anyone confirm that this is isolated? If so, I can report the bug to bugzilla.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

审判长 2024-09-09 09:30:56

不能将数组创建语法与对象初始化语法混合在一起。删除 { }.

当您编写时:

int[] row = new int[indices.Count];

您正在创建一个大小为 indices.Count 并初始化为默认值的新数组。

当您编写时:

int[] row = new int[] { 1, 2, 3, 4 };

您正在创建一个数组,然后将其内容初始化为值 [1,2,3,4]。数组的大小是根据元素的数量推断的。它的简写是:

int[] row = new int[4];
row[0] = 1;
row[1] = 2;
row[2] = 3;
row[3] = 4;

数组仍然首先初始化为默认值,这种语法只是提供了一种简写,以避免必须自己编写这些额外的赋值。

You can't mix array creation syntax with object initialization syntax. Remove the { }.

When you write:

int[] row = new int[indices.Count];

You are creating a new array of size indices.Count initialized to default values.

When you write:

int[] row = new int[] { 1, 2, 3, 4 };

You are creating an array and then initializing it's content to the values [1,2,3,4]. The size of the array is inferred from the number of elements. It's shorthand for:

int[] row = new int[4];
row[0] = 1;
row[1] = 2;
row[2] = 3;
row[3] = 4;

The array is still first initialized to defaults, this syntax just provides a shorthand to avoid havind to write those extra assignments yourself.

写下不归期 2024-09-09 09:30:56

以下代码由于同样的原因在 Windows/.NET/LINQPad 上无法编译:

void Main()
{
    int[] row = new int[indices.Count]{};
    row[2] = 10;
    row.Dump();
}

// Define other methods and classes here
public class indices {
    public static int Count = 5;
}

但是,从声明中删除对象初始化 ({}) 使其可以工作。

The following code fails to compile for the same reason on Windows/.NET/LINQPad:

void Main()
{
    int[] row = new int[indices.Count]{};
    row[2] = 10;
    row.Dump();
}

// Define other methods and classes here
public class indices {
    public static int Count = 5;
}

However, removing the object initialisation from the declaration ({}) makes it work.

多像笑话 2024-09-09 09:30:56

在 C# 中,如果要声明空数组,语法应为:

int[] row = new int[indices.Count];

In C#, if you want to declare an empty array the syntax should be:

int[] row = new int[indices.Count];

我不在是我 2024-09-09 09:30:56

因为当您使用数组初始化语法并指定数组的大小时,

int[] arr = new int[5]{1,2,3,4,5};

数组的大小是多余的信息。编译器可以从初始化列表推断大小。正如其他人所说,您可以创建空数组:

int[] arr = new int[5];

或使用初始化列表:

int[] arr = {1,2,3,4,5};

Because when you to use use array initialization syntax AND specify the size of the array

int[] arr = new int[5]{1,2,3,4,5};

The size of the array is superfluous information. The compiler can infer the size from the initialization list. As others have said, you either create empty array:

int[] arr = new int[5];

or use the initialization list:

int[] arr = {1,2,3,4,5};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文