Back to questions
JuniorTheoryCommon
What does the program print?
var a = new House();
var b = new House();
var c = new House();
Console.WriteLine(House.Count + " " + a.Number);
class House
{
public static int Count { get; private set; }
public int Number { get; private set; }
public House() => Number = ++Count;
}3 1
- Expecting
1 1: astaticproperty is one for the whole class, so every constructor call raises the sameCount. - Expecting
a.Numberto followCount: the assignment copies the value at that moment, so the first house keeps1.
- What would the program print if
Countwere notstatic? - Why does
a.Countnot compile whileHouse.Countdoes?