简体   繁体   English

结构的多维数组C#

[英]multidimensional array of structs C#

Hello everyone :) I lately started learning C# and wanted to create minesweeper game. 大家好:)我最近开始学习C#,想创建扫雷游戏。 I had pattern, done before in C++ and Java ( just console ) and it seemed to be fine in C# as well. 我有模式,以前在C ++和Java中完成过(只是console),在C#中也似乎还不错。

But there is an error I can't get rid of. 但是有一个我无法消除的错误。

Warning CS0649 Field 'Minefield.field' is never assigned to, and will always have its default value null 警告CS0649字段'Minefield.field'从未分配,并且将始终具有其默认值null

Here is part of code I have trouble with: 这是我遇到麻烦的部分代码:

 struct  Field
{
    public int mine;
    public int mines_around;
    public State status;

};

class Minefield
{
    Random rand = new Random();
    const int Rows = 10;
    const int Columns = 10;
    Field[][] field;
    int Difficulty;

    public Minefield(int Diff)//from 1 to 10

I've seen there is way of creating arrays like Field[,] field = new Field[10,10] But as I tried it the same error occured. 我已经看到了创建像Field [,] field = new Field [10,10]这样的数组的方法,但是当我尝试它时,发生了同样的错误。 Any ideas from more experienced C# programmers ? 有经验的C#程序员有什么想法吗?

It is not an error, but simply notice from the static code analysis. 这不是错误,而只是从静态代码分析中注意到。 It will disappear as soon as you use it, eg by assigning values or reading them. 使用后,它将消失,例如通过分配值或读取它们。

Keep in mind while struct is a value type an array of structs is not. 请记住,虽然struct是一个值类型,但不是一个数组结构。 Therefore unline private int a it is not assigned automatically. 因此,unline private int a不会自动分配。

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

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