Back to questions
JuniorTheoryCommon
What happens when this program is compiled and run?
var a = new House(3);
var b = new House();
Console.WriteLine(a.Floors + " " + b.Floors);
class House
{
public int Floors;
public House(int floors) => Floors = floors;
}It does not compile
- Expecting
3 0: a class gets a default constructor only while it has no constructor of its own, andHouse(int floors)removes it. - Expecting a failure only when
new House()runs: the compiler sees before the start that no constructor fits, so nothing is printed.
- How would you change
Houseso thatnew House()builds a house with one floor? - Why does the compiler stop adding a default constructor once you write your own?