Back to questions
JuniorTheoryCommon
What does the program print (lines separated by spaces)?
Spot a = new Spot();
a.X = 7;
Spot b = new Spot();
b.X = 7;
Console.WriteLine(a == b);
b = a;
Console.WriteLine(a == b);
class Spot { public int X; }False True
- Expecting
True True: for a class,==compares references by default, and two objects with equal fields are still two different objects. - Expecting
False False:b = amakesblead to the objectaleads to, so the second comparison is between one object and itself.
- What would the program print if
Spotwere astructand both lines useda.Equals(b)? - Why does
==compare the text of twostringobjects instead of their references?