reference type

V

vincente13

i understand that in csharp "string" and "object" are reference type...

For e.g. in this case
myString is passed to a method and it changes the value of the string..
if string is a reference type, isnt it passed to the method by
reference and the second WriteLine should be "edited" instead of "hello
world" ?



String myString = "hello world";

Console.WriteLine("String before function : " + myString);
doSomethingToString(myString);
Console.WriteLine("String afer function : " + myString);

static void doSomethingToString(string myString)
{
myString = "edited";
}
 
G

gpg

In C# arguments are passed by value - thus only the copy myString in
function scope is modified.

If you want to modify an argument, you must use the 'ref' keyword to
pass the argument by reference.

For example
String myString = "hello world";

Console.WriteLine("String before function : " + myString);
doSomethingToString(ref myString);
Console.WriteLine("String afer function : " + myString);

static void doSomethingToString(ref string myString)
{
myString = "edited";
}

will result in myString being modified in the main scope.

GPG
 
H

Hans Kesting

i understand that in csharp "string" and "object" are reference type...
For e.g. in this case
myString is passed to a method and it changes the value of the string..
if string is a reference type, isnt it passed to the method by
reference and the second WriteLine should be "edited" instead of "hello
world" ?

String myString = "hello world";

Console.WriteLine("String before function : " + myString);
doSomethingToString(myString);
Console.WriteLine("String afer function : " + myString);

static void doSomethingToString(string myString)
{
myString = "edited";
}

No, because with the statement
myString = "edited";
you don't change the contents of the original string, but create a new
string (containing "edited") and changing the reference of the local
variable myString to point to that new string.
When you return from that function, the original reference is still in
place, so you see the original text. (Note: there are two separate
'myString' variables here: one 'global', the other local to the method)

Hans Kesting
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

i understand that in csharp "string" and "object" are reference type...

For e.g. in this case
myString is passed to a method and it changes the value of the string..

No, actually it doesn't change the value of the string. Strings are
immutable in .NET, meaning that the value of the string object never
changes.

Instead a completely new string object is used and the reference to that
object replaces the old reference.
if string is a reference type, isnt it passed to the method by
reference and the second WriteLine should be "edited" instead of "hello
world" ?

No, all parameters are passed by value by default. For a reference type
that means that the reference is passed by value, i.e. the value of the
reference is copied.

In the method you are using the copy of the reference, so eventhough you
replace the reference with the reference to the new string, that does
not change the original reference.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top