A project you can extend is one that you understand. In this lesson you add a pending count to the planner's summary, which means changing a record, a calculation and the report. That's a small version of how most real changes look.
One new field, three places
A record's constructor changes when its fields do. Add a field to Summary and the compiler flags every call that's now wrong, which gives you a list of what to update. This is one of the benefits of a compiled language, so let the errors guide you.
var summary = new Summary(2, 35);
Console.WriteLine(summary);
record Summary(int Tasks, int Planned, int Pending);Check your prediction
error CS7036Pass a third argument to fix the error.
Your turn
Add a pending count
Using the planner below, add a Pending field to Summary so that it counts tasks not yet done. Update Summarize and print a new line, Pending: 2 tasks, after Remaining in Program.cs. For the supplied input you should see two pending tasks, and the rest of the report shouldn't change.
If you also run the checks, they'll need updating, as their new Summary(...) calls have one fewer value than the record now expects. That's the compiler helping you again.
Hint 1: the idea
Pending is counted in the same loop that adds up the minutes, as a third running value.
Hint 2: a first step
Change the record, and then read what the compiler complains about. The return new Summary(...) line is the first thing to fix.
Show one solution
The change lives in Planner.cs, inside Planner.Summarize, plus one line in Program.cs. So that it can run here, this listing cuts Planner down to the parts Summarize needs and puts everything in one file.
var planner = new Planner();
planner.Add(new StudyTask("Read chapter", 25, false));
planner.Add(new StudyTask("Review notes", 10, true));
planner.Add(new StudyTask("Practice", 180, false));
Summary summary = planner.Summarize();
Console.WriteLine($"Remaining: {summary.Remaining} minutes");
Console.WriteLine($"Pending: {summary.Pending} tasks");
class Planner
{
private readonly List<StudyTask> tasks = new List<StudyTask>();
public void Add(StudyTask task)
{
tasks.Add(task);
}
public Summary Summarize()
{
int planned = 0;
int completed = 0;
int pending = 0;
foreach (StudyTask task in tasks)
{
planned += task.Minutes;
if (task.Done)
{
completed += task.Minutes;
}
else
{
pending++;
}
}
return new Summary(tasks.Count, planned, completed, planned - completed, pending);
}
}
record StudyTask(string Title, int Minutes, bool Done);
record Summary(int Tasks, int Planned, int Completed, int Remaining, int Pending);Remaining: 205 minutes
Pending: 2 tasksThe new count is added to the loop that already visits every task, in the else of the existing if, so a task is either completed or pending and never both. Adding Pending as a new field at the end of the record means existing code that reads the other fields keeps working.
More ideas, if you want to go further with the project: call Complete before printing the report and see which totals move; add a method that returns only the unfinished tasks; or print the longest task's title. Each one of these needs a new check.
Optional local setup
Everything so far has run in your browser, and you can stop here. To work on the project on your own computer, download the C# study planner. It contains the same source files as the browser tabs, along with a project file, sample input and a README. Your browser edits aren't included, so copy across anything you want to keep.
Install the .NET 10 SDK from dotnet.microsoft.com, open a terminal in the extracted folder and run:
dotnet --version
dotnet run < sample-input.txt
dotnet run --property:Checks=true< supplies a file as standard input. In Windows PowerShell, use Get-Content sample-input.txt | dotnet run instead. The project file StudyPlanner.csproj sets up the project, including nullable checking everywhere and the choice of Program.cs or Checks.cs as the starting file.
<Compile Include="Planner.cs" />
<Compile Include="Program.cs" Condition="'$(Checks)' != 'true'" />
<Compile Include="Checks.cs" Condition="'$(Checks)' == 'true'" />A normal .NET project has one starting file and compiles every .cs file it finds, so you don't normally need these lines. They're here so the application and checks can share one folder, as they do in the browser. Larger projects usually keep tests in a separate project that uses a framework such as xUnit.
The browser runtime has some limits that a local install doesn't: there are no threads, input has to be supplied before Run, and files don't last beyond a single Run. Those are limits of running in a browser, not of C#.
Completion is saved in this browser only. It does not measure mastery.