Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Constructors | Introduction to Object-Oriented Programming (OOP)
C# Beyond Basics

bookChallenge: 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

index.cs

copy
123456789101112131415161718192021222324
using System; class Dog { public string name; public string breed; public int age; // Write constructor code below this line // 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(); } }

In order to assign the argument values to the fields without raising any error you'll have to use this operator, since the arguments are the same name as the fields.

index.cs

index.cs

copy
12345678910111213141516171819202122232425262728
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?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 10

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you show me how to write the constructor for the Dog class?

What does the complete Dog class look like with the constructor?

Can you explain how the `this` operator works in this context?

Awesome!

Completion rate improved to 2.04

bookChallenge: Constructors

Swipe to show menu

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

index.cs

copy
123456789101112131415161718192021222324
using System; class Dog { public string name; public string breed; public int age; // Write constructor code below this line // 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(); } }

In order to assign the argument values to the fields without raising any error you'll have to use this operator, since the arguments are the same name as the fields.

index.cs

index.cs

copy
12345678910111213141516171819202122232425262728
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?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 10
some-alt