编辑数组也会更改另一个数组

时间:2021-04-26 16:12:52

标签: c# arrays console

所以我在 Windows 控制台中制作了一个小游戏(只是为了好玩和测试理论),但我在使用 DrawGame() 方法时遇到了问题。

我正在创建一个新的字符串数组 (string[] _CompleteMap),其值是从 CurrentMap.MapData(也是一个字符串数组)分配的:

string[] _CompleteMap = CurrentMap.MapData;

我使用 CurrentMap.MapData 作为空白地图,然后更改字符串中的字符来代表玩家和生物。 但是当我对 _CompleteMap 进行更改时,它似乎也在更改 CurrentMap.MapData...

中的值

我不知道为什么会发生这种情况,它会留下用户走过的痕迹。

任何支持都会有所帮助,如果需要,我可以发布代码。

using System;
using System.Text;
using System.Threading;
using ConsoleAdventure.Engine.BaseClasses;
using Database = ConsoleAdventure.Engine.Database;

namespace ConsoleAdventure
{
    class Program
    {
        private static bool whileGameScene = true;
        private static Engine.BaseClasses.MapBase CurrentMap;

        static unsafe void Main(string[] args)
        {
            // Load and populate the databases.
            Database.ItemDB.LoadItemDB();
            Database.MapDB.LoadMapDB();

            // Set demo map
            CurrentMap = Database.MapDB.Maps[0];

            // Hide cursor
            Console.CursorVisible = false;

            // Start a new thread to capture the user inputs
            new Thread(() =>
                        {
                            while (true)
                            {
                                Thread.CurrentThread.IsBackground = true;

                                ConsoleKeyInfo cki = Console.ReadKey();

                                if (cki.Key == ConsoleKey.A && Player.Data.Location.X > 0)
                                    Player.Data.Location.X--;
                                else if (cki.Key == ConsoleKey.D)
                                    Player.Data.Location.X++;
                                else if (cki.Key == ConsoleKey.W && Player.Data.Location.Y > 0)
                                    Player.Data.Location.Y--;
                                else if (cki.Key == ConsoleKey.S)
                                    Player.Data.Location.Y++;

                                CheckWarpPoints();
                            }        
                        }).Start();

            // Draw the game
            while (whileGameScene)
            {
                DrawGame();
            }
        }

        /// <summary>
        /// Check if player has stood on a warp zone
        /// </summary>
        private static void CheckWarpPoints()
        {
            foreach (MapBase.WarpPoint warp in Database.MapDB.Maps[Player.Data.CurrentMap].WarpPoints)
            {
                if (Player.Data.Location.X == warp.From.X && Player.Data.Location.Y == warp.From.Y)
                {
                    Player.Data.CurrentMap = warp.ToMapIndex;
                    Player.Data.Location = warp.To;
                }
            }
        }

        public static void DrawGame()
        {
            string[] _CompleteMap = CurrentMap.MapData;

            StringBuilder _sbRow = new StringBuilder();

            foreach (var _object in CurrentMap.MapObjects)
            {
                _sbRow = new StringBuilder(_CompleteMap[_object.CurrentLocation.Y]);
                _sbRow[_object.CurrentLocation.X] = _object.Icon;
                _CompleteMap[_object.CurrentLocation.Y] = _sbRow.ToString();
            }

            _sbRow = new StringBuilder(_CompleteMap[Player.Data.Location.Y]);
            _sbRow[Player.Data.Location.X] = 'X';
            _CompleteMap[Player.Data.Location.Y] = _sbRow.ToString();

            for (int i = 0; i < _CompleteMap.Length; i++)
            {
                Console.SetCursorPosition(0, i);
                Console.WriteLine(_CompleteMap[i]);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:3)

因为数组是引用类型,所以当你这样做时:

string[] _CompleteMap = CurrentMap.MapData;

您实际上是在创建一个新变量,该变量指向与 CurrentMap.MapData 相同的内存位置,并且您通过 _CompleteMap 对该位置所做的任何更改也将在您通过以下方式引用该位置时显示出来CurrentMap.MapData

相反,您可以根据现有数组的内容创建一个新数组:

string[] _CompleteMap = CurrentMap.MapData.ToArray();

现在它指向了不同的位置,因此对该位置的数组的修改不会影响原始位置的数组。


然而,有一点需要注意,如果 MapData 是一个引用类型的数组,并且您使用 ToArray() 制作它的副本,那么即使您现在有一个包含相同的数据,如果您操作数组副本中的一个对象,则对该对象的更改也会出现在原始数组中。

这是因为我们做了一个拷贝。如果我们不想要这种行为,那么我们需要做一个复制,在那里我们还为数组中的每个对象创建新的(克隆的)实例。有什么要研究的!!

例如:

class Person
{
    public string Name { get; set; }
}

class Program
{
    static void Main()
    {
        // Original array has a person named John
        Person[] people = new [] { new Person { Name = "John" } };

        // Create a shallow copy of the array
        Person[] peopleCopy = people.ToArray();

        // Change John's name in the copy
        peopleCopy[0].Name = "James";

        // At this point, people[0].Name is also "James", since it's the same person!

        // However changes to the array itself are discreet
        peopleCopy[0] = new Person { Name = "Mary" };

        // At this point, people[0].Name is still "James", since we did
        // not modify the existing object, but instead created a new one
    }
}
相关问题