|
-
July 6th, 2011, 06:44 PM
#1
[RESOLVED] References and copying objects
I'm used to the C++ world where there's a fairly clear distinction between dealing with an object directly and dealing with a pointer or reference... so maybe I'm overthinking things somewhat, but I just want to be clear in my understanding before I confuse myself with the 'everything is a reference' notion....
I have a Message class, and I need to be able to make (deep) copies of it, such that if I have Message A, and make a copy of it, Message B, A and B have identical content, but any change to B has no effect on A. Easiest to show some code:
Code:
class Variable {}
class Message
{
public String Title {get; set;}
public String Text {get; set;}
public Dictionary<String, Variable> Variables
{
get { return _variables; }
}
private Dictionary<String, Variable> _variables;
Message() {}
Message(Message copy)
{
//Is this.Title a new string? or referencing the same string as copy.Title?
Title = copy.Title;
Text = copy.Text;
//is this a deep copy or not?
_variables = new Dictionary<String, Variable>(copy._variables);
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|