1

I have a list of Character objects I made and a list of Cells which can contain one character. Is it possible for my Character object to be added to the list and be assigned to a cell and changes I make to it in the party list or the cell to effect the object in both place? I don't really know how the pointers will work out for this. I figure what will happen is the object in the list is a separate object from the one assigned to the cell.

Sorry my code is very large so I don't want to post it all here but the Character I am talking about is a custom class I made. I suppose my question really boles down to two questions. When I put something in a list is that changing where the pointer points or is that a new object all together. Also can I have multiple pointer if I add something to a list then assign that to another instance of the character object will referencing the Character from the Cell Object be the Same as referencing my Character from the other list object.

4

2 に答える 2

2

あなたCharacterがクラス(つまり参照型)である限り、本質的にオブジェクトへの参照を保存していCharacterます。オブジェクトへの参照を介してオブジェクトに加えた変更は、他の参照を介してオブジェクトにアクセスしたときに表示されます。

例:

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

var c = new Character();
var c2 = c;
var arr1 = new Character[] { c };
var arr2 = new Character[] { c };

arr1[0].Name = "Foo";
Console.WriteLine(arr2[0].Name); // "Foo"
Console.WriteLine(c2.Name); // also "Foo"
于 2013-04-06T18:36:18.873 に答える
1

したがって、基本的には Character を参照型として扱います。

内部に文字を含む独自のラッパー オブジェクトを作成するだけです。

public MyCharacter
{
public char character{get;set;}
}

オブジェクトは参照によって処理されるため、うまくいくはずです。

于 2013-04-06T18:36:11.853 に答える