Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我有一个循环,它用值初始化对象列表,但一旦退出循环,对象就会变得相同_C# - Fatal编程技术网

C# 我有一个循环,它用值初始化对象列表,但一旦退出循环,对象就会变得相同

C# 我有一个循环,它用值初始化对象列表,但一旦退出循环,对象就会变得相同,c#,C#,正如标题所示,我正在通过循环初始化列表中的对象。但当循环退出时,它们都变得相同。在循环过程中,我可以看到它们不一样。但当循环退出时,它们将更改为最后输入的对象 public List<ElevationLayout> layoutList = new List<ElevationLayout>(); public int layoutNumber { get; set; } public int worldWidth { get; set; }

正如标题所示,我正在通过循环初始化列表中的对象。但当循环退出时,它们都变得相同。在循环过程中,我可以看到它们不一样。但当循环退出时,它们将更改为最后输入的对象

public List<ElevationLayout> layoutList = new List<ElevationLayout>();
    public int layoutNumber { get; set; }
    public int worldWidth { get; set; }

    public Random seed { get; set; }
    public XYSize dimLeft { get; set; }


//I have narrowed down the problem to this method
    //==========================================================================================================================================================
    //==========================================================================================================================================================
    //==========================================================================================================================================================

    public void init(World world) {
        dimLeft = new XYSize();
        ElevationLayout layout = new ElevationLayout();
        dimLeft.y = 0;
        dimLeft.x = world.size.x;
        seed = new Random((int)DateTime.Now.Ticks);
        worldWidth = (int)((world.size.x / 6.4) + (world.size.x / 64) - 1);
        layoutNumber =  worldWidth + seed.Next(-2, 3);
        for (int i = 0; i < layoutNumber; i++)
        {
            layout.type = seed.Next(0, 2);
            layout.width = (world.size.x / layoutNumber) + seed.Next(0, ((dimLeft.x / layoutNumber) / 2) + 1);

            if (layout.width > dimLeft.x)
            {
                layout.width = dimLeft.x;
            }

            dimLeft.x -= layout.width;
            layout.height = seed.Next(world.size.y / 16, (world.size.y / 4) + 1);

            if (layout.height > dimLeft.y)
            {
                layout.height = dimLeft.y;
            }


            this.layoutList.Add(layout);
            Console.Write(this.layoutList[i].type); // here the objects are different

            if ((world.size.y -= layout.height) > dimLeft.y)
            {
                dimLeft.y = (world.size.y - layout.height);
            }

            if (dimLeft.x <= 0)
            {
                layoutNumber = i;
            }
        }
        Console.WriteLine();
                for (int y = 0; y < layoutNumber; y++)
                    Console.Write(this.layoutList[y].type); //but as soon as i exit the loop they are the same
    }

    //==============================================================================================================
    //==============================================================================================================
    //==============================================================================================================
public List layoutList=new List();
公共int布局编号{get;set;}
公共int worldWidth{get;set;}
公共随机种子{get;set;}
公共XYSize dimLeft{get;set;}
//我已把问题缩小到这种方法
//==========================================================================================================================================================
//==========================================================================================================================================================
//==========================================================================================================================================================
公共void init(世界){
dimLeft=新的XYSize();
ElevationLayout=新的ElevationLayout();
dimLeft.y=0;
dimLeft.x=world.size.x;
种子=新的随机((int)DateTime.Now.Ticks);
worldWidth=(int)((world.size.x/6.4)+(world.size.x/64)-1);
layoutNumber=worldWidth+seed.Next(-2,3);
对于(int i=0;idimLeft.x)
{
layout.width=dimLeft.x;
}
dimLeft.x-=layout.width;
layout.height=seed.Next(world.size.y/16,(world.size.y/4)+1);
如果(layout.height>dimLeft.y)
{
layout.height=dimLeft.y;
}
this.layoutList.Add(布局);
Console.Write(this.layoutList[i].type);//这里的对象不同
if((world.size.y-=layout.height)>dimLeft.y)
{
y=(world.size.y-layout.height);
}

如果(dimLeft.x,问题是您只在循环之前创建一个对象实例,并设置该对象的属性。 修正:

for(int i=0;i

基本上,在循环中创建对象,以便在每次迭代中分配一个新对象。

问题在于,在循环之前只创建一个对象实例,并设置该对象的属性。 修正:

for(int i=0;i

基本上,在循环内创建对象,以便在每次迭代中分配一个新对象。

您将一次又一次地向列表中添加相同的布局,而不是每次都创建一个新的布局实例。移动代码以在循环内创建布局对象。

您将一次又一次地向列表中添加相同的布局,而不是比每次创建一个新的layout实例要好得多。移动代码在循环中创建layout对象。

谢谢你修复了它。你能解释一下,如果我每次都更改对象的值,然后添加它,为什么会出现问题吗?(作为一名编程学生,我很好奇。)请参阅-当您将对象添加到列表中时,您添加的是对该对象的引用,而不是它的副本。因此,您的列表中包含了对单个对象(即内存中的单个对象)的引用。谢谢,这很有意义。我将阅读该文章(?)你发布了。谢谢你修复了它。你能解释一下,如果我每次都更改对象的值,然后添加它,为什么会出现问题吗?(作为一名编程学生,我只是好奇。)请参阅-当您将对象添加到列表中时,您是在添加对该对象的引用,而不是它的副本。因此,您的列表中充满了对单个对象(即内存中的单个对象)的引用。谢谢,这很有意义。我将阅读您发布的文章(?)。
    for (int i = 0; i < layoutNumber; i++)
    {
          ElevationLayout layout = new ElevationLayout();