“Boxing” (Problem)
What will the following code display?
1
struct Foo
2
{
3
int value;
4
public override string ToString()
5
{
6
if (value == 2)
7
return "Baz";
8
return (value++ == 0) ? "Foo" : "Bar";
9
}
10
}
11
void Main()
12
{
13
var foo = new Foo();
14
Console.WriteLine(foo);
15
Console.WriteLine(foo);
16
object bar = foo;
17
object qux = foo;
18
object baz = bar;
19
Console.WriteLine(baz);
20
Console.WriteLine(bar);
21
Console.WriteLine(baz);
22
Console.WriteLine(qux);
23
}
Copied!
Solution
Copy link