16. Next Steps

You now know the core of C#: program structure, data types, operators, decisions, loops, methods, strings, arrays and collections, classes, inheritance and interfaces, exceptions, generics and delegates, LINQ, async code and file handling. The best way to make this stick is to build small programs: a number-guessing game, a to-do list that saves to a file, a contact book using List<T> and JSON, a student-marks report using LINQ, and a console bank account with classes and exceptions.

After the basics, the usual path is: unit testing (xUnit or NUnit), dependency injection, Entity Framework Core for databases, ASP.NET Core for web APIs and websites (controllers, Razor Pages, minimal APIs), configuration and logging, authentication and authorisation, and design principles such as SOLID and clean architecture. Practise by building small console programs first, then a web API that stores data in a database.

Key points to remember

  • Read code written by others, and write something every day. Small projects teach more than long tutorials.
  • Use Git from the start and commit often. Keep your projects on GitHub.
  • Learn the debugger: breakpoints, stepping (F10 / F11), the Watch and Locals windows, and conditional breakpoints.
  • SOLID: Single responsibility, Open/closed, Liskov substitution, Interface segregation, Dependency inversion.
  • Official resources: learn.microsoft.com/dotnet/csharp (the C# documentation and tour), the .NET API browser, and the C# language reference.
  • Write tests for your methods early. Code that is easy to test is usually well designed.

Example 1: A minimal ASP.NET Core web API

C#
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/hello/{name}", (string name) => $"Hello, {name}!");
app.MapGet("/api/numbers", () => new[] { 1, 2, 3 });

app.Run();
Show output Output
GET /hello/Thabo   ->  Hello, Thabo!
GET /api/numbers   ->  [1,2,3]

Example 2: A unit test with xUnit

C#
public class CalculatorTests
{
    [Fact]
    public void Add_ReturnsSum()
    {
        Assert.Equal(5, Calculator.Add(2, 3));
    }

    [Theory]
    [InlineData(1, 1, 2)]
    [InlineData(-1, 1, 0)]
    public void Add_Works(int a, int b, int expected)
    {
        Assert.Equal(expected, Calculator.Add(a, b));
    }
}

public static class Calculator
{
    public static int Add(int a, int b) => a + b;
}
Show output Output
dotnet test
Passed!  - Failed: 0, Passed: 3, Skipped: 0

Example 3: Dependency injection

C#
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddScoped<IGreeter, Greeter>();     // register the service
var app = builder.Build();

app.MapGet("/greet", (IGreeter greeter) => greeter.Greet("Ana"));   // injected
app.Run();

public interface IGreeter { string Greet(string name); }
public class Greeter : IGreeter
{
    public string Greet(string name) => $"Welcome, {name}";
}
Show output Output
GET /greet  ->  Welcome, Ana