Stage 0: Initial code

Stage 0: Initial code

Highlights:

  • Cat, dog, and parrot objects created with specific attributes and behaviors

  • Primary constructor used to assign values to properties, set clause removed to maintain encapsulation

  • Code repetition identified as a maintenance headache; DRY principle will be introduced in next stage in order to avoid it!

  • OOP principles highlighted as important for efficient and maintainable applications.

Cat.cs

namespace MyApplication.Animals;

public class Cat(string name)
{
    public string Name { get; } = name;

    public void Eat()
    {
        Console.WriteLine($"{Name} is eating...");
    }

    public void Climb()
    {
        Console.WriteLine($"{Name} is climbing on a roof!");
    }
}

Dog.cs

namespace MyApplication.Animals;

public class Dog(string name)
{
    public string Name { get; } = name;

    public void Eat()
    {
        Console.WriteLine($"{Name} is eating...");
    }

    public void Climb()
    {
        Console.WriteLine($"{Name} is climbing on steps!");
    }
}

Parrot.cs

namespace MyApplication.Animals;

public class Parrot(string name)
{
    public string Name { get; } = name;

    public void Eat()
    {
        Console.WriteLine($"{Name} is eating...");
    }

    public void Climb()
    {
        Console.WriteLine($"{Name} is climbing on a tree!");
    }

    public void Fly()
    {
        Console.WriteLine($"{Name} is Flying!");
    }
}

Program.cs

using MyApplication.Animals;

var cat = new Cat("Tom");
Console.WriteLine($"{cat.Name} created!");
cat.Eat();
cat.Climb();
Console.WriteLine();

var dog = new Dog("Mike");
Console.WriteLine($"{dog.Name} created!");
dog.Eat();
dog.Climb();
Console.WriteLine();

var parrot = new Parrot("Glory");
Console.WriteLine($"{parrot.Name} created!");
parrot.Eat();
parrot.Climb();
parrot.Fly();
Console.WriteLine();

Last updated