Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.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.对象的行为_C#_Object_Reference - Fatal编程技术网

C# 请从参考资料中解释C.对象的行为

C# 请从参考资料中解释C.对象的行为,c#,object,reference,C#,Object,Reference,我有一个带有一个参数的方法,它是我创建的类的实例——Chessfield,它包含三个表——一个是整数,两个是布尔 在这个方法中,我还有一个Chessfield列表,它是我的方法的返回对象 我正在修改chessfield,以便从引用中删除对象,并在每次更改后将其添加到list.AddChessfield多次 在最后返回对象,使列表包含多个对象的棋子场所有实例都是相同的取消我的更改 我读过类似的主题,并尝试将'ref'放在参数之前,以及我继续此方法的位置。 也没有成功,我尝试在方法内部创建Chess

我有一个带有一个参数的方法,它是我创建的类的实例——Chessfield,它包含三个表——一个是整数,两个是布尔

在这个方法中,我还有一个Chessfield列表,它是我的方法的返回对象

我正在修改chessfield,以便从引用中删除对象,并在每次更改后将其添加到list.AddChessfield多次

在最后返回对象,使列表包含多个对象的棋子场所有实例都是相同的取消我的更改

我读过类似的主题,并尝试将'ref'放在参数之前,以及我继续此方法的位置。 也没有成功,我尝试在方法内部创建Chessfield实例,并从引用中为其分配Chessfield对象,然后对内部创建的对象进行更改

我怎样才能解决它?最后,我需要接收对象列表,每个对象都从引用中的原始对象稍加修改

祝你今天愉快

p、 如果代码将有帮助,然后我剪切和粘贴的一般想法代码

编辑:

乔尔和我很好!不同之处在于,对象内部是表,这使得问题更加复杂,因为与原始对象相比,我对这些表所做的更改很少。为了更清楚,我粘贴了我的代码:

public class Chessfield
{

    public int[] pieces = new int[64];
    public bool[] blacks = new bool[64];
    public bool[] whites = new bool[64];

    public Chessfield(int[] pieces, bool[] blacks, bool[] whites)
    {
        this.pieces = pieces;
        this.blacks = blacks;
        this.whites = whites;
    }

    public Chessfield()
    {

    }

}
方法如下所示:

    static public List<Chessfield> MakeAllMovesForWhites(Chessfield chessfieldModel)
    {
        List<Chessfield> listOfPossibleMoves = new List<Chessfield>(); // list containing chessfields with changed position of figures
        int indexOfCurrentPosition = 0; //start with field 0 (most top left)

        foreach (bool singleEnemyChecker in chessfieldModel.whites) //iterate all fields,  table of whites contain information if white field stand on the field (true, otherwise false), 
        {
            if (singleEnemyChecker == true) //so algorithm will proceed only fields with white figure
            {
                int kindOfPiece = chessfieldModel.pieces[indexOfCurrentPosition]; // (table pieces contain information which kind of figure stand on particular field 0 -> empty, 1 -> soldier, 2-> tower, 3 -> horse etc...
                switch (kindOfPiece)// (based on figure at field it is going to predict all possible moves
                {

                    case 2: // tower case
                        if (indexOfCurrentPosition % 8 != 0) // check if the field is not most left, otherwise leave 
                        {
                            int localIndexIterator = indexOfCurrentPosition; //localIndex iterate all possible moves in left direction
                            while (localIndexIterator % 8 != 0) // checking if tower is standing on the most left field
                            {
                                localIndexIterator = localIndexIterator - 1; //iterate all possible moves of tower for left direction
                                if (chessfieldModel.pieces[localIndexIterator] == 0) //if there are no figures on checking field proceed:
                                {
                                    chessfieldModel.pieces[indexOfCurrentPosition] = 0; // erase tower from original position
                                    chessfieldModel.whites[indexOfCurrentPosition] = false; // and mark that white tower is not standing there anymore
                                    chessfieldModel.pieces[localIndexIterator] = 2; // put tower on new place
                                    chessfieldModel.whites[localIndexIterator] = true; // and mark that on new place there is white figure

                                    listOfPossibleMoves.Add(chessfieldModel); // here I add changed object of chessfield to list

                                    chessfieldModel.pieces[indexOfCurrentPosition] = 2; // here I come back to original chessfield
                                    chessfieldModel.whites[indexOfCurrentPosition] = true;
                                    chessfieldModel.pieces[localIndexIterator] = 0;
                                    chessfieldModel.whites[localIndexIterator] = false;
                                }
                                else //if there is figure at checking field
                                    break; //leave this case
                            }
                        }
                        if (indexOfCurrentPosition % 8 != 7) // right direction case
                        {
                          // here is similar code to the sample above
                        }
                        if (indexOfCurrentPosition / 8 != 0) //top direction case
                        {
                          // here is similar code to the sample above
                        }
                        if (indexOfCurrentPosition / 8 != 7) //bottom direction case
                        {
                          // here is similar code to the sample above
                        }
                        break;

       // here are another figures horse and so on...

                }
            }
            indexOfCurrentPosition++; // go to next field...
        }

        return listOfPossibleMoves; //return list of changed chessfields 
    }
public class ChessField 
{
   public bool b1;
   public bool b2;
   public int i1;
}

public List<ChessField> Method(ChessField c)
{
    var result = new List<ChessField>();
    for (int i = 0;i<3;i++)
    {
       c.i1 = i;
       result.Add(c);
    }
    return result;
}
public List<ChessField> Method(ChessField c)
{
    var result = new List<ChessField>();
    for (int i = 0;i<3;i++)
    {
       result.Add(new ChessField() {b1 = c.b1, b2 = c.b2, i1 = i});
    }
    return result;
}
我明白问题所在。还有乔尔,你解释得很好!谢谢

我第一次尝试解决这个问题是在我问之前:

Chessfield abc = new Chessfield();
abc = chessfieldModel;
abc.pieces[indexOfCurrentPosition] = 0;
abc.whites[indexOfCurrentPosition] = true;
abc.pieces[localIndexIterator] = 2;
abc.whites[localIndexIterator] = false;
listOfPossibleMoves.Add(abc);
失败。我在每种情况下都尝试过,为每个图形和每个方向创建。顺便说一句,在国际象棋中有33种不同的情况下,人物如何移动,所以我在上面的33个地方有这段代码,但有时我会把不同的东西放在桌子上。。。但是,如果左侧没有碎片,塔这样的数字可以向左移动1,2,3,4,5,6,7个字段。。这就是我必须创建新实例的问题,我不知道如何创建,因为我必须创建唯一的实例,对其进行一些更改,然后添加到列表中。。总是独一无二的,但在不同的情况下

我也尝试过你的解决方案Joel,但问题是我需要对原来的chessfield 4行做一些更改,但对不同的数字做不同的更改。 但我尝试创建一个新实例,将其添加到列表中,然后在已经在列表中时对其进行更改。不起作用,逻辑也不正确

listOfPossibleMoves.Add(new Chessfield() { pieces = chessfieldModel.pieces, blacks = chessfieldModel.blacks, whites = chessfieldModel.whites });
listOfPossibleMoves[listOfPossibleMoves.Count - 1].pieces[indexOfCurrentPosition] = 0;
listOfPossibleMoves[listOfPossibleMoves.Count - 1].whites[indexOfCurrentPosition] = false;
listOfPossibleMoves[listOfPossibleMoves.Count - 1].pieces[localIndexIterator] = 2;
listOfPossibleMoves[listOfPossibleMoves.Count - 1].whites[localIndexIterator] = true;
编辑:也许回到我的第一个方法,但是我如何为在同一个地方创建的对象创建唯一的名称呢?你能推荐一些技巧或者我在这种情况下能做些什么吗

谢谢Joel和所有人:
祝大家白天或晚上愉快

查看问题中的代码会很有帮助,但听起来您正在做这样的事情:

    static public List<Chessfield> MakeAllMovesForWhites(Chessfield chessfieldModel)
    {
        List<Chessfield> listOfPossibleMoves = new List<Chessfield>(); // list containing chessfields with changed position of figures
        int indexOfCurrentPosition = 0; //start with field 0 (most top left)

        foreach (bool singleEnemyChecker in chessfieldModel.whites) //iterate all fields,  table of whites contain information if white field stand on the field (true, otherwise false), 
        {
            if (singleEnemyChecker == true) //so algorithm will proceed only fields with white figure
            {
                int kindOfPiece = chessfieldModel.pieces[indexOfCurrentPosition]; // (table pieces contain information which kind of figure stand on particular field 0 -> empty, 1 -> soldier, 2-> tower, 3 -> horse etc...
                switch (kindOfPiece)// (based on figure at field it is going to predict all possible moves
                {

                    case 2: // tower case
                        if (indexOfCurrentPosition % 8 != 0) // check if the field is not most left, otherwise leave 
                        {
                            int localIndexIterator = indexOfCurrentPosition; //localIndex iterate all possible moves in left direction
                            while (localIndexIterator % 8 != 0) // checking if tower is standing on the most left field
                            {
                                localIndexIterator = localIndexIterator - 1; //iterate all possible moves of tower for left direction
                                if (chessfieldModel.pieces[localIndexIterator] == 0) //if there are no figures on checking field proceed:
                                {
                                    chessfieldModel.pieces[indexOfCurrentPosition] = 0; // erase tower from original position
                                    chessfieldModel.whites[indexOfCurrentPosition] = false; // and mark that white tower is not standing there anymore
                                    chessfieldModel.pieces[localIndexIterator] = 2; // put tower on new place
                                    chessfieldModel.whites[localIndexIterator] = true; // and mark that on new place there is white figure

                                    listOfPossibleMoves.Add(chessfieldModel); // here I add changed object of chessfield to list

                                    chessfieldModel.pieces[indexOfCurrentPosition] = 2; // here I come back to original chessfield
                                    chessfieldModel.whites[indexOfCurrentPosition] = true;
                                    chessfieldModel.pieces[localIndexIterator] = 0;
                                    chessfieldModel.whites[localIndexIterator] = false;
                                }
                                else //if there is figure at checking field
                                    break; //leave this case
                            }
                        }
                        if (indexOfCurrentPosition % 8 != 7) // right direction case
                        {
                          // here is similar code to the sample above
                        }
                        if (indexOfCurrentPosition / 8 != 0) //top direction case
                        {
                          // here is similar code to the sample above
                        }
                        if (indexOfCurrentPosition / 8 != 7) //bottom direction case
                        {
                          // here is similar code to the sample above
                        }
                        break;

       // here are another figures horse and so on...

                }
            }
            indexOfCurrentPosition++; // go to next field...
        }

        return listOfPossibleMoves; //return list of changed chessfields 
    }
public class ChessField 
{
   public bool b1;
   public bool b2;
   public int i1;
}

public List<ChessField> Method(ChessField c)
{
    var result = new List<ChessField>();
    for (int i = 0;i<3;i++)
    {
       c.i1 = i;
       result.Add(c);
    }
    return result;
}
public List<ChessField> Method(ChessField c)
{
    var result = new List<ChessField>();
    for (int i = 0;i<3;i++)
    {
       result.Add(new ChessField() {b1 = c.b1, b2 = c.b2, i1 = i});
    }
    return result;
}
或者我喜欢的风格是:

public IEnumerable<ChessField> Method(ChessField c)
{
   return Enumerable.Range(0, 3)
                    .Select(i => new ChessField() {b1 = c.b1, b2 = c.b2, i1 = i});
}

代码会很有帮助。您需要基于第一个对象创建一个新对象,而不是更改现有对象。您可能希望使您的类不可变,然后公开创建新实例的方法,这些新实例是带有所需更改的克隆。这与字符串的工作方式非常相似。