Groups | Blog | Home
all groups > dotnet clr > july 2003 >

dotnet clr : New string or object reference?


Célio Cidral Junior
7/31/2003 3:04:21 PM
Hello!

See the code below:

public class Customer
{
private string name;

public Customer(string name) : base()
{
this.name = name;
}

public string Name
{
get { return this.name; }
set { // some implementation ... }
}
}

public class SomeClass
{
public void Main()
{
string name;
Customer customer = new Customer("John");

// Does it returns the string's reference or its copy?
name = customer.Name;

Console.WriteLine(name);
}
}

That's it, a very simple code. Here goes the question:
Does the <Customer.Name> property return the object reference of the
string at the <Customer.name> field, or a copy of that string?

Thank you in advance!

---
Celio Cidral Junior
ccidral@expresso.com.br

Célio Cidral Junior
8/1/2003 8:27:15 AM
Jon, these explanations about memory and parameters are just what I was
looking for. Very good pages!

Thank you.


"Jon Skeet" <skeet@pobox.com> escreveu na mensagem
news:MPG.19943ef74321fc7598a25c@news.microsoft.com...
[quoted text, click to view]

Jon Skeet
8/1/2003 10:10:50 AM
[Apparently this didn't go through before. Not sure why...]

[quoted text, click to view]

<snip>

[quoted text, click to view]

It returns a copy of the reference to the same object. It's very, very
important that you understand the difference between reference types
and value types. Have a look at
http://www.pobox.com/~skeet/csharp/parameters.html and
http://www.pobox.com/~skeet/csharp/memory.html

Hopefully they'll clarify things for you. It's one of those things that
is really simple once it's clicked, but maddening and complicated until
it has...

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet/
Andre
8/5/2003 4:22:18 AM
Ah.. looking at the thread I knew Jon would've replied... surely,
history repeats itself... and in case of reference types, it seems to be
repeating every 24 hours :) Jon to the rescue!

-Andre

[quoted text, click to view]
AddThis Social Bookmark Button