Back to questions
JuniorTheoryCommon
What does the program print?
var a = new House("red");
House b = a;
var c = new House("red");
b.Color = "blue";
Console.WriteLine(a.Color + " " + c.Color);
class House
{
public string Color { get; set; }
public House(string color) => Color = color;
}blue red
- Expecting
red red:House b = a;copies the reference, not the house, so a change throughbis seen througha. - Expecting two red houses to be one house: each
newbuilds a separate object, so repainting the house ofaleavescred.
- What would the program print if
bgot its own house withnew? - What does
a == bgive afterHouse b = a;, and why?