Build your first classes to create digital pets that have names, ages, and special abilities! (Ages 8-18)
CSharp-Lesson-Five/
โโโ .git/
โโโ .gitignore
โโโ CSharp-Lesson-Five.csproj
โโโ Program.cs
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.
- 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
- 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
- What to do: Replace your
Program.cswith the code below. It adds a simplePetclass with three fields, thenMaincreates 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"
- What to do: Replace your
Program.cswith the version below. ThePetclass now hasSpeak()andPlay()methods, andMaincalls 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"
- What to do: Replace your
Program.cswith 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"
- What to do: Replace your
Program.cswith the version below.Mainnow creates aPet[]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"
- What to do: Replace your
Program.cswith the version below.NameandAgeare 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"
- 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
- 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.
- "Type 'Program' already defines a member called 'Main'" โ You have two
Mainmethods. Make sure you replaced the whole file at each step instead of pasting on top of the old code. There should be exactly onestatic void Main(string[] args)inProgram.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 theclass Pet { ... }andclass Program { ... }braces. - "Object reference not set to an instance of an object" โ You forgot
new Pet(...)when creating a pet. Always usenewwhen 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), sonew Pet()with no arguments stops working. Pass the three arguments likenew Pet("Buddy", 3, "Dog").
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}!");
}
}