5 min read
Introduction to Classes & Objects
Prerequisites: Method overloading & optional parameters
Syntax
class Dog {
public string Name = "";
public void Bark() {
Console.WriteLine($"{Name} says Woof!");
}
}
Example
Dog myDog = new Dog();
myDog.Name = "Rex";
myDog.Bark();
Expected output
Rex says Woof!
Practice
Create a Cat class with a Name field and a Meow() method that
prints "<Name> says Meow!".
Show solution
class Cat {
public string Name = "";
public void Meow() {
Console.WriteLine($"{Name} says Meow!");
}
}