Practice: Constructors
A simple class called Dog
is given. Create a constructor which takes in the arguments name
, breed
, age
, and initializes the fields from the values of arguments.
index.cs
this
operator, since the arguments are the same name as the fields.
using System;
class Dog {
public string name;
public string breed;
public int age;
// Write constructor code below this line
public Dog(string name, string breed, int age) {
this.name = name;
this.breed = breed;
this.age = age;
}
// Write constructor code above this line
public void bark() {
Console.WriteLine("Woof!");
}
}
public class ConsoleApp
{
public static void Main(string[] args)
{
Dog dog = new Dog("Dobby", "Dobermann", 4);
dog.bark();
}
}
Everything was clear?
Course Content
C# Beyond Basics
C# Beyond Basics
Practice: Constructors
A simple class called Dog
is given. Create a constructor which takes in the arguments name
, breed
, age
, and initializes the fields from the values of arguments.
index.cs
this
operator, since the arguments are the same name as the fields.
using System;
class Dog {
public string name;
public string breed;
public int age;
// Write constructor code below this line
public Dog(string name, string breed, int age) {
this.name = name;
this.breed = breed;
this.age = age;
}
// Write constructor code above this line
public void bark() {
Console.WriteLine("Woof!");
}
}
public class ConsoleApp
{
public static void Main(string[] args)
{
Dog dog = new Dog("Dobby", "Dobermann", 4);
dog.bark();
}
}
Everything was clear?