Skip to content
AgileLogics
4 min read

Constructors

Prerequisites: Introduction to classes & objects

class Dog {
    public string Name;
    public int Age;

    public Dog(string name, int age) {
        Name = name;
        Age = age;
    }
}

Dog rex = new Dog("Rex", 3);

Common mistakes

  • Once any constructor is defined, C# — like Java — stops generating the free parameterless constructor automatically.