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.

2
  • Show us your code, and tell us more about your application and how you want it to behave. char's are primitive types, so they're basically values and will have value semantics. But the answer to your question is going to depend on what your code looks like. Commented Apr 6, 2013 at 18:36
  • 1
    Take a look at these two articles by Jon Skeet - Parameter Passing in C# and Memory in .NET - what goes where. Commented Apr 6, 2013 at 18:41

2 Answers 2

2

As long as your Character is a class (i.e. a reference type) then you are essentially storing references to Character objects. Any changes you make to an object through a reference to it will be visible when the object is accessed through any other reference.

Example:

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"
Sign up to request clarification or add additional context in comments.

2 Comments

So can I have two references to the same object? I have a list that holds it then I have another custom object that also needs access to that object but I don't know when I need to reference my Character from the custom object and when I need to reference it from the list.
@maxinfet: Yes. Whenever you use the assignment operator or you pass "the object" to a function a new reference to the same object is created.
1

So basically you want to treat Characters as a reference type.

Just create your own wrapper object which contains a character inside it.

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

Objects are treated by reference, so that should work for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.