O
Oliver
I'm an expert C# programmer (honest) but...
the following code prints 'Oranges', not 'ORANGES' - this confuses me
because I thought reference types (eg string) where passed
by...err..reference - so changing where the reference points to in a
method ought to change the behaviour outside the method.
....using the 'ref' keyword makes this happen.
it's almost as if references passed to methods are *copied*.
so - what's going on here, and why aren't all reference types passed
by reference by default....?
thanks,
Oliver.
class Class1
{
static void Main(string[] args)
{
string fruit = "Oranges";
DoSomeWork(fruit);
Console.WriteLine("fruit: " + fruit);
}
private static void DoSomeWork(string myString)
{
string newString = myString.ToUpper();
myString = newString;
}
}
the following code prints 'Oranges', not 'ORANGES' - this confuses me
because I thought reference types (eg string) where passed
by...err..reference - so changing where the reference points to in a
method ought to change the behaviour outside the method.
....using the 'ref' keyword makes this happen.
it's almost as if references passed to methods are *copied*.
so - what's going on here, and why aren't all reference types passed
by reference by default....?
thanks,
Oliver.
class Class1
{
static void Main(string[] args)
{
string fruit = "Oranges";
DoSomeWork(fruit);
Console.WriteLine("fruit: " + fruit);
}
private static void DoSomeWork(string myString)
{
string newString = myString.ToUpper();
myString = newString;
}
}