Skip to main content
Published / updated

C# Programming Foundation

Before you start

You need: stage 1, and a working Visual Studio installation.

Time: 36–44 classes. This is the longest stage and everything after it depends on it.

Learning objective

Build the C# competence every later stage assumes — types, control flow, classes, collections, LINQ, exceptions and async.

Topics

  • C# and .NET fundamentals
  • Types, and why money is decimal
  • Control flow, and why condition order decides correctness
  • Classes, encapsulation and interfaces
  • Collections and LINQ
  • Exceptions, files and JSON
  • async/await
  • Debugging with breakpoints

What this stage covers

This is the longest stage of the path — 36 to 44 classes — and everything after it depends on it. An API is C#. A repository is C#. A service is C#. Weakness here surfaces as confusion in stage 5, not stage 2.

ConceptUsed later in
Classes and propertiesEntities and DTOs, stages 4 and 5
InterfacesThe repository pattern, stage 4
Collections and LINQEvery service method, stage 5
decimalEvery fee amount, stages 3 to 10
Nullable typesAbsent exam results, throughout
ExceptionsAPI error handling, stage 5
async/awaitEvery database and HTTP call
BreakpointsStage 8, and every day after

Three rules established here recur in every remaining stage:

  • Money is decimal, never double. Floating-point arithmetic does not sum exactly, and a fee report off by paise is always this.
  • An absent exam result is null, never 0, and the absent check comes first in any grading chain. Stored as 0, the class average is silently wrong.
  • Look-ups can return nothing. FirstOrDefault plus a null check, not First.

Worked flow: the fee receipt

At this stage the receipt exists as C# classes and one calculation — no database, no API, no screen.

public class FeeReceipt
{
public string ReceiptNumber { get; set; }
public string StudentName { get; set; }
public string RollNumber { get; set; }
public string ClassName { get; set; }
public decimal AmountPaid { get; set; }
public DateTime PaidOn { get; set; }
public PaymentMode PaymentMode { get; set; }
public decimal BalanceAfterPayment { get; set; }
}
public class ReceiptService
{
public FeeReceipt Build(Student student, FeeAccount account, FeePayment payment)
{
if (student == null)
{
throw new ArgumentNullException(nameof(student));
}

if (account == null)
{
throw new ArgumentNullException(nameof(account));
}

decimal balanceAfter = account.TotalFees - account.DiscountAmount - account.PaidAmount;

FeeReceipt receipt = new FeeReceipt();
receipt.ReceiptNumber = payment.ReceiptNumber;
receipt.StudentName = student.Name;
receipt.RollNumber = student.RollNumber;
receipt.ClassName = $"{student.ClassName}-{student.Section}";
receipt.AmountPaid = payment.Amount;
receipt.PaidOn = payment.PaidOn;
receipt.PaymentMode = payment.PaymentMode;
receipt.BalanceAfterPayment = balanceAfter;

return receipt;
}
}

Every value here is decimal. Every guard clause comes first. The class does one thing, and it can be unit tested with no database, no API and no browser — which is exactly why the calculation lives in a service and not in a screen.

Where to learn it

TopicRead
.NET, the SDK, the first projectTrack 03 — .NET and setup
Types, decimal, nullable, parsingTrack 03 — Types and operators
Conditions, loops, methodsTrack 03 — Control flow and methods
Classes, encapsulation, interfacesTrack 03 — Classes and OOP
Collections and genericsTrack 03 — Collections and generics
LINQTrack 03 — LINQ
Exceptions, files, JSONTrack 03 — Errors, files and JSON
async/awaitTrack 03 — Async and await
Debugging and unit testsTrack 03 — Debugging and quality
The console capstoneTrack 03 — Student Marks Application

Do the whole of Track 03, including the capstone. The Student Marks Application is where absent handling, decimal arithmetic and unit testing become habits rather than facts.

Stage exercises

From the guided path syllabus:

Build and test one CRUD feature. In a console application, implement add, list, update and deactivate for Student, held in a List<Student> behind an IStudentRepository. Later stages replace the list with SQL Server without changing the interface — which is the point.

Model the receipt. Write FeeReceipt, FeeAccount and FeePayment as classes, and ReceiptService.Build as above. Unit test it for a full payment, a partial payment, and a payment on an account with a discount.

Debugging drills

Fix a mapping error. Deliberately assign PaidOn where DueDate belongs and watch the receipt show a wrong date with no exception. Find it with a breakpoint on Build and the Locals window.

Trace a loop and a method call. Loop over 40 exam results with a conditional breakpoint on result.StudentId == 12, and read the Call Stack to see which method supplied the argument.

Fix null, index and parsing errors. Trigger each deliberately — a repository returning null, a CSV row with a missing field, int.Parse on a roll number — and fix each with the two-line guard rather than a try/catch.

Practice

  1. Work through Track 03's ten articles and their practice lists.
  2. Build the Student Marks Application capstone.
  3. Model FeeReceipt, FeeAccount and FeePayment, and write ReceiptService.Build.
  4. Unit test the receipt for full payment, partial payment and a discounted account.
  5. Compute a fee total as double and as decimal over a thousand payments, and compare.
  6. Write the grading chain with the absent check last, run it for an absent student, then fix the order.
  7. Implement IStudentRepository over an in-memory list, with add, list, update and deactivate.
  8. Write a FirstOrDefault lookup with a null check, then replace it with First and see what a missing student does.
  9. Debug a wrong receipt date using a breakpoint and Locals rather than print statements.
  10. Convert a synchronous repository method to async Task<T> and await it from Main.

You can now

  • Write, debug and explain structured C#
  • Model School entities as classes with encapsulation
  • Use collections, generics, delegates and LINQ
  • Handle exceptions without hiding them
  • Write async methods correctly
  • Debug in Visual Studio and write a unit test

Review questions

  1. Why is every money value in this stage decimal?
  2. Why does ReceiptService.Build live in a service rather than in a screen?
  3. What does IStudentRepository let you change later without touching the service?
  4. Why must the absent check come first?

Next: SQL Server foundation