M
Maxim
Hi!
A have a string variable (which is a reference type).
Now I define my Method like that:
void MakeFullName(string sNamePrivate)
{
sNamePrivate+="Gates"
}
The calling code:
string sName="Bill";
MakeFullName(sName);
My variable sName wasn't changed (the same "Bill"). String is a
reference type and I would expect
that it will be passed by reference without defining the method
parameter as "ref". Only after defining it as "ref" I get the expected
result ("BillGates").
void MakeFullName1(ref string sNamePrivate)
{
sNamePrivate+="Gates"
}
The calling code:
string sName="Bill";
MakeFullName1(ref sName);
In the following article Jesse Liberty explicitly says, that reference
parameters are passed by reference:
http://www.ondotnet.com/pub/a/dotnet/2002/02/11/csharp_traps.html?page=2
"Value types are passed to methods by value (a copy is made) while
reference types are effectively passed by reference."
Does it mean, that unless I declare my paramter as "ref" my value
outside the mehtod is never changed?
Thanks for you help.
Maxim
A have a string variable (which is a reference type).
Now I define my Method like that:
void MakeFullName(string sNamePrivate)
{
sNamePrivate+="Gates"
}
The calling code:
string sName="Bill";
MakeFullName(sName);
My variable sName wasn't changed (the same "Bill"). String is a
reference type and I would expect
that it will be passed by reference without defining the method
parameter as "ref". Only after defining it as "ref" I get the expected
result ("BillGates").
void MakeFullName1(ref string sNamePrivate)
{
sNamePrivate+="Gates"
}
The calling code:
string sName="Bill";
MakeFullName1(ref sName);
In the following article Jesse Liberty explicitly says, that reference
parameters are passed by reference:
http://www.ondotnet.com/pub/a/dotnet/2002/02/11/csharp_traps.html?page=2
"Value types are passed to methods by value (a copy is made) while
reference types are effectively passed by reference."
Does it mean, that unless I declare my paramter as "ref" my value
outside the mehtod is never changed?
Thanks for you help.
Maxim