Z
z_learning_tester
I'm reading the MS press C# book and there seems to be a contradiction.
Please tell me which one is correct, 1 or 2.
Thanks!
Jeff
1. First it gives the code below saying that it prints 0 then 42.
They say that 42 is printed the second time since the value was wrapped in a
class and therefore became passed by reference.
(sorry for any typos I am a newbie here ;-)
class Application
{
static void Entrance()
{
WrappedInt wi = new WrappedInt();
Console.WriteLine(wi.Number); //This one prints 0
they say...
Pass.Reference(wi);
Console.WriteLine(wi.Number) //And this one
prints 42.
}
}
class WrappedInt
{
public int Number;
}
class Pass
{
public static void Reference(WrappedInt param)
{
param.Number = 42;
}
}
--------------------------------
2. Then it goes on to say that whether it is passed by value or reference,
the original arguement is unchanged unless you use 'ref' or 'out'.
So what gives? Which is correct, 1 or 2? If 2 is correct, then the example
they gave in 1 shouldn't be valid right? I may be missing something you
would find obvious.
Thanks for any explanations!
Please tell me which one is correct, 1 or 2.
Thanks!
Jeff
1. First it gives the code below saying that it prints 0 then 42.
They say that 42 is printed the second time since the value was wrapped in a
class and therefore became passed by reference.
(sorry for any typos I am a newbie here ;-)
class Application
{
static void Entrance()
{
WrappedInt wi = new WrappedInt();
Console.WriteLine(wi.Number); //This one prints 0
they say...
Pass.Reference(wi);
Console.WriteLine(wi.Number) //And this one
prints 42.
}
}
class WrappedInt
{
public int Number;
}
class Pass
{
public static void Reference(WrappedInt param)
{
param.Number = 42;
}
}
--------------------------------
2. Then it goes on to say that whether it is passed by value or reference,
the original arguement is unchanged unless you use 'ref' or 'out'.
So what gives? Which is correct, 1 or 2? If 2 is correct, then the example
they gave in 1 shouldn't be valid right? I may be missing something you
would find obvious.
Thanks for any explanations!