C#声明2D数组 [英] C# declaring a 2D array

查看:127
本文介绍了C#声明2D数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在C#中设置一个2d数组,作为一个迷宫来移动角色,我在初始化该数组时遇到了一些问题,我正在尝试执行以下操作

I am trying to setup a 2d array in C# to act as a maze to move a character around, I am having a few issues initialising the array, I am trying to do the below

但是InitialiseMaze方法表示未声明迷宫

but the InitialiseMaze method is saying the maze is not declared

任何人都可以建议

谢谢

simon

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GameMan
{
    public class Maze
    {
       #region Variables
       static int[,] maze;

    #endregion
    #region Constructors/Destructors
    public Maze()
    {
        InitaliseMaze();
    }
    ~Maze()
    {
    }
    #endregion

    #region Methods
    public void InitaliseMaze()
    {

         maze = {
                          {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},    
                          {0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0},     
                          {0, 0, 3, 0, 0, 2, 0, 0, 0, 2, 0, 2, 0, 0, 0, 2, 0, 0, 3, 0, 0},     
                          {0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0},    
                          {0, 0, 2, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 2, 0, 0},    
                          {0, 0, 2, 2, 2, 2, 0, 2, 2, 2, 0, 2, 2, 2, 0, 2, 2, 2, 2, 0, 0},     
                          {0, 0, 0, 0, 0, 2, 0, 0, 0, 1, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0},    
                          {0, 0, 0, 0, 0, 2, 0, 1, 1, 1, 4, 1, 1, 1, 0, 2, 0, 0, 0, 0, 0},     
                          {0, 0, 0, 0, 0, 2, 0, 1, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0},     
                          {1, 1, 1, 1, 1, 2, 1, 1, 0, 0, 0, 0, 0, 1, 1, 2, 1, 1, 1, 1, 1},
                          {0, 0, 0, 0, 0, 2, 0, 1, 0, 0, 0, 0, 0, 1, 1, 2, 0, 0, 0, 0, 0},
                          {0, 0, 0, 0, 0, 2, 0, 1, 1, 1, 1, 1, 1, 1, 0, 2, 0, 0, 0, 0, 0},
                          {0, 0, 0, 0, 0, 2, 0, 1, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0},
                          {0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0},
                          {0, 0, 2, 0, 0, 2, 0, 0, 0, 2, 0, 2, 0, 0, 0, 2, 0 ,0, 2, 0, 0},
                          {0, 0, 3, 2, 0, 2, 2, 2, 2, 2, 5, 2, 2, 2, 2, 2, 0, 2, 3, 0, 0},
                          {0, 0, 0, 2, 0, 2, 0, 2, 0, 0, 0, 0, 0, 2, 0, 2, 0, 2, 0, 0, 0},
                          {0, 0, 2, 2, 2, 2, 0, 2, 2, 2, 0, 2, 2, 2, 0, 2, 2, 2, 2, 0, 0},
                          {0, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0},
                          {0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0},
                          {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
                      };
    }
    #endregion
}

}

推荐答案

除了变量声明之外,您不能像这样初始化数组.但是,更改很简单:

You can't initialize an array like that other than in a variable declaration. However, the change is simple:

maze = new int[,] { 
   // As before
};

助手:

  • 看起来maze应该是 instance 变量,而不是静态变量.毕竟,您每次创建Maze
  • 实例时都要对其进行初始化.
  • 您无缘无故拥有终结器.在C#中很少需要(或确实建议)终结器
  • It looks like maze should be an instance variable rather than a static variable. After all, you're initializing it each time you create an instance of Maze
  • You have a finalizer for no reason. Finalizers are very rarely required (or indeed advisable) in C#

这篇关于C#声明2D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆