Back to questions
JuniorTheoryCommon
What does the program print?
Box box = new Box();
Change(box);
Console.WriteLine(box.Value);
void Change(Box b)
{
b.Value = 2;
b = new Box();
b.Value = 3;
}
class Box { public int Value; }2
- Expecting
3: objects are passed by value too, and the value is a reference;b = new Box()changes only the method's copy, soboxkeeps its object with2. - Expecting
0: the method gets a copy of the reference, not of the object, sob.Value = 2changes the very objectboxleads to.
- What would the program print if the parameter were declared as
ref Box band called withChange(ref box)? - What would it print if
Boxwere astructinstead of aclass?