Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

ย 

History

5 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐ŸŽฏ C# Objects & Classes Intro Project

Lesson Snapshot

Build your first classes to create digital pets that have names, ages, and special abilities! (Ages 8-18)

Folder & File Map

CSharp-Lesson-Five/
โ”œโ”€โ”€ .git/
โ”œโ”€โ”€ .gitignore
โ”œโ”€โ”€ CSharp-Lesson-Five.csproj
โ””โ”€โ”€ Program.cs

How to use the code snippets

Each step below shows the complete contents of Program.cs after you finish that step. When a step says "replace your Program.cs with this", select all (Ctrl+A / Cmd+A) inside Program.cs, delete it, then paste the new code. That keeps you from ending up with two Main methods or leftover code from earlier steps.

Step-by-Step Build Guide

1. Fork and Clone the Repository โœ…

  • What to do: Go to https://github.com/PhillyCoderDojo/CSharp-Lesson-Five โ†’ Click "Fork" โ†’ Open GitHub Desktop โ†’ Clone your fork
  • File(s) touched: Creating project folder
  • Code snippet: None
  • Why it matters: Get your own space to create objects
  • Git command: git clone https://github.com/YOUR-USERNAME/CSharp-Lesson-Five.git

2. Open Project in Rider โœ…

  • What to do: Open Rider โ†’ File โ†’ Open โ†’ Select your cloned folder โ†’ Click OK
  • File(s) touched: Loading project
  • Code snippet: None
  • Why it matters: Ready to build your first class!
  • Git command: None

3. Create Your First Class ๐Ÿ•

  • What to do: Replace your Program.cs with the code below. It adds a simple Pet class with three fields, then Main creates one pet and describes it.
  • File(s) touched: Program.cs
  • Code snippet (full file):
namespace CSharp_Lesson_Five;

class Pet
{
    public string Name;
    public int Age;
    public string Type;
}

class Program
{
    static void Main(string[] args)
    {
        Pet myPet = new Pet();
        myPet.Name = "Buddy";
        myPet.Age = 3;
        myPet.Type = "Dog";

        Console.WriteLine($"Meet {myPet.Name}, a {myPet.Age}-year-old {myPet.Type}!");
    }
}
  • Why it matters: Classes are blueprints for creating objects
  • Git command: git add . && git commit -m "Create first Pet class with fields"

4. Add Methods to Your Class ๐ŸŽพ

  • What to do: Replace your Program.cs with the version below. The Pet class now has Speak() and Play() methods, and Main calls them so you can actually see your pet do things.
  • File(s) touched: Program.cs
  • Code snippet (full file):
namespace CSharp_Lesson_Five;

class Pet
{
    public string Name;
    public int Age;
    public string Type;

    public void Speak()
    {
        if (Type == "Dog")
            Console.WriteLine($"{Name} says: Woof! Woof!");
        else if (Type == "Cat")
            Console.WriteLine($"{Name} says: Meow! Meow!");
        else
            Console.WriteLine($"{Name} makes a sound!");
    }

    public void Play()
    {
        Console.WriteLine($"{Name} is playing and having fun!");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Pet myPet = new Pet();
        myPet.Name = "Buddy";
        myPet.Age = 3;
        myPet.Type = "Dog";

        Console.WriteLine($"Meet {myPet.Name}, a {myPet.Age}-year-old {myPet.Type}!");
        myPet.Speak();
        myPet.Play();
    }
}
  • Why it matters: Methods make objects do things
  • Git command: git add . && git commit -m "Add Speak and Play methods to Pet class"

5. Create a Constructor ๐Ÿ”ง

  • What to do: Replace your Program.cs with the version below. A constructor lets you set a pet's name, age, and type in one line when you create it.
  • File(s) touched: Program.cs
  • Code snippet (full file):
namespace CSharp_Lesson_Five;

class Pet
{
    public string Name;
    public int Age;
    public string Type;

    public Pet(string name, int age, string type)
    {
        Name = name;
        Age = age;
        Type = type;
        Console.WriteLine($"๐ŸŽ‰ {Name} the {Type} has been created!");
    }

    public void Speak()
    {
        if (Type == "Dog")
            Console.WriteLine($"{Name} says: Woof! Woof!");
        else if (Type == "Cat")
            Console.WriteLine($"{Name} says: Meow! Meow!");
        else
            Console.WriteLine($"{Name} makes a sound!");
    }

    public void Play()
    {
        Console.WriteLine($"{Name} is playing and having fun!");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Pet dog = new Pet("Buddy", 3, "Dog");
        Pet cat = new Pet("Whiskers", 2, "Cat");

        dog.Speak();
        cat.Speak();
        dog.Play();
    }
}
  • Why it matters: Constructors set up objects with starting values
  • Git command: git add . && git commit -m "Add constructor to Pet class"

6. Build a Pet Care Game ๐ŸŽฎ

  • What to do: Replace your Program.cs with the version below. Main now creates a Pet[] array of three pets and loops through them.
  • File(s) touched: Program.cs
  • Code snippet (full file):
namespace CSharp_Lesson_Five;

class Pet
{
    public string Name;
    public int Age;
    public string Type;

    public Pet(string name, int age, string type)
    {
        Name = name;
        Age = age;
        Type = type;
        Console.WriteLine($"๐ŸŽ‰ {Name} the {Type} has been created!");
    }

    public void Speak()
    {
        if (Type == "Dog")
            Console.WriteLine($"{Name} says: Woof! Woof!");
        else if (Type == "Cat")
            Console.WriteLine($"{Name} says: Meow! Meow!");
        else
            Console.WriteLine($"{Name} makes a sound!");
    }

    public void Play()
    {
        Console.WriteLine($"{Name} is playing and having fun!");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("=== Pet Care Game ===");

        Pet[] pets = {
            new Pet("Max", 4, "Dog"),
            new Pet("Luna", 2, "Cat"),
            new Pet("Charlie", 1, "Rabbit")
        };

        Console.WriteLine("\nMeet your pets:");
        foreach (Pet pet in pets)
        {
            Console.WriteLine($"โ€ข {pet.Name} ({pet.Age} years old, {pet.Type})");
        }

        Console.WriteLine("\nTime to play!");
        foreach (Pet pet in pets)
        {
            pet.Speak();
            pet.Play();
            Console.WriteLine();
        }
    }
}
  • Why it matters: Objects work together in real programs
  • Git command: git add . && git commit -m "Create pet care game with multiple objects"

7. Add Properties with Validation ๐Ÿ›ก๏ธ

  • What to do: Replace your Program.cs with the version below. Name and Age are now properties with a backing field, and they clean up bad values (empty name โ†’ "Unknown", negative age โ†’ 0).
  • File(s) touched: Program.cs
  • Code snippet (full file):
namespace CSharp_Lesson_Five;

class Pet
{
    private string _name;
    private int _age;
    public string Type;

    public string Name
    {
        get { return _name; }
        set { _name = string.IsNullOrEmpty(value) ? "Unknown" : value; }
    }

    public int Age
    {
        get { return _age; }
        set { _age = value < 0 ? 0 : value; }
    }

    public Pet(string name, int age, string type)
    {
        Name = name;
        Age = age;
        Type = type;
        Console.WriteLine($"๐ŸŽ‰ {Name} the {Type} has been created!");
    }

    public void Speak()
    {
        if (Type == "Dog")
            Console.WriteLine($"{Name} says: Woof! Woof!");
        else if (Type == "Cat")
            Console.WriteLine($"{Name} says: Meow! Meow!");
        else
            Console.WriteLine($"{Name} makes a sound!");
    }

    public void Play()
    {
        Console.WriteLine($"{Name} is playing and having fun!");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("=== Pet Care Game ===");

        Pet[] pets = {
            new Pet("Max", 4, "Dog"),
            new Pet("Luna", 2, "Cat"),
            new Pet("Charlie", 1, "Rabbit"),
            new Pet("", -2, "Hamster")
        };

        Console.WriteLine("\nMeet your pets:");
        foreach (Pet pet in pets)
        {
            Console.WriteLine($"โ€ข {pet.Name} ({pet.Age} years old, {pet.Type})");
        }

        Console.WriteLine("\nTime to play!");
        foreach (Pet pet in pets)
        {
            pet.Speak();
            pet.Play();
            Console.WriteLine();
        }
    }
}
  • Why it matters: Properties protect data from bad values
  • Git command: git add . && git commit -m "Add properties with validation"

8. Push Your Object-Oriented Code ๐Ÿš€

  • What to do: In GitHub Desktop โ†’ Commit changes โ†’ Push origin
  • File(s) touched: Uploading to GitHub
  • Code snippet: None
  • Why it matters: Share your first object-oriented program
  • Git command: git push origin main

Run & Test โ–ถ๏ธ

  • Press Ctrl+F5 or click green โ–ถ๏ธ button in Rider
  • Expected output: Pet creation messages (one per pet), a "Meet your pets" list, then each pet speaks and plays. After Step 7, the bad pet (empty name, age -2) shows as "Unknown" with age 0.

Common errors and fixes

  • "Type 'Program' already defines a member called 'Main'" โ€” You have two Main methods. Make sure you replaced the whole file at each step instead of pasting on top of the old code. There should be exactly one static void Main(string[] args) in Program.cs.
  • "The entry point of the program is global code; ignoring 'Main(string[])' entry point" โ€” Same root cause: there's a stray line of code outside any method (often a leftover Console.WriteLine("Hello, World!"); from the starter). Delete anything outside the class Pet { ... } and class Program { ... } braces.
  • "Object reference not set to an instance of an object" โ€” You forgot new Pet(...) when creating a pet. Always use new when creating objects.
  • "No suitable method found to override" or "Pet does not contain a constructor that takes 0 arguments" โ€” After Step 5 the Pet class only has a constructor that takes (name, age, type), so new Pet() with no arguments stops working. Pass the three arguments like new Pet("Buddy", 3, "Dog").

Bonus Challenge ๐Ÿ”ฅ

Create a Player class for a simple RPG game!

Hint:

class Player
{
    public string Name;
    public int Health;
    public int Level;

    public Player(string name)
    {
        Name = name;
        Health = 100;
        Level = 1;
    }

    public void Attack(string target)
    {
        Console.WriteLine($"{Name} attacks {target}!");
    }

    public void LevelUp()
    {
        Level++;
        Health += 10;
        Console.WriteLine($"{Name} leveled up to {Level}!");
    }
}

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages