iDevJunkie
iDevJunkie

Reputation: 797

Reference Types. Do I understand this correctly?

This is embarrassing. I learned C# a long time ago and am just now attempting to understand this question about classes (since they are reference types, of course).

Here's the question: If I create two new instances of a class called Person and named one instance P and the other Q (who cares why I'd call it Q) and set P.Name to "James" and then set Q.Name to "Suzie", would P.Name be set to "Suzie"? Or am I not understanding this correctly?

Thanks everyone

Thanks everyone for helping me with this. I assumed what was explained to me was the case. But the explanations in the tutorials I read weren't clear and I haven't had a computer in a few months so as to have tested it out myself.

P.s I chose the first right answer that was clear to me. But noticed several. Thanks again for everyone's help.

Upvotes: 3

Views: 126

Answers (5)

Richard Schneider
Richard Schneider

Reputation: 35477

The short answer is NO, changing Q.Name will no affect P.Name. As in

 var p = new Person();
 var q = new Person();
 p.Name = "James";
 q.Name = "Suzie";

However, if q points to the p instance, then changing q would also change p. As in:

 var p = new Person();
 var q = p;
 p.Name = "James";
 q.Name = "Suzie";

Both q and q Name are now "Suzie".

With data initialisers you can write the first example as:

var p = new Person { Name = "James" };
var q = new Person { Name = "Suzie" };

which I think is much easier to read.

Upvotes: 5

Ajay
Ajay

Reputation: 6590

No this is not, because both are P and Q different instance of class Person. Both object or instance define on different memoery location. see this tutorial

Upvotes: 3

Dave Zych
Dave Zych

Reputation: 21887

If you create two instances? No, they will be 2 separate objects.

Person person1 = new Person();
person1.Name = "James";
Person person2 = new Person();
person2.Name = "Suzie";
Console.WriteLine(person1.Name);
Console.WriteLine(person2.Name);

That will print out James and then Suzie, since they are 2 different objects. If you did this, however:

Person person1 = new Person();
person1.Name = "James";
Person person2 = person1;
person2.Name = "Suzie";
Console.WriteLine(person1.Name);
Console.WriteLine(person2.Name);

That will print Suzie and Suzie. This is because person1 and person2 both reference the same object.

Upvotes: 2

Vikram
Vikram

Reputation: 1627

This will definitely not change the P.Name to Suzie as you have created two instances of the same class, it means that you have allocated two different memory locations on heap for these two instance.

Upvotes: 1

Simon Whitehead
Simon Whitehead

Reputation: 65069

Example:

// p and q are separate in the below example..

Person p = new Person();
Person q = new Person();

p.Name = "James";
q.Name = "Suzie";

Console.WriteLine(p.Name); // "James"
Console.WriteLine(q.Name); // "Suzie"

// both p and q refer to the same object, so both are "Suzie" in the below example
Person p = new Person();
Person q = p;

p.Name = "James";
q.Name = "Suzie";

Console.WriteLine(p.Name); // "Suzie"
Console.WriteLine(q.Name); // "Suzie"

Upvotes: 3

Related Questions