Pick a console app, web API or library; get .sln, .csproj and tests as a zip.
Make a solution skeleton
Choose what to build and a code style; the folder tree, file notes and layer picture follow. Click a file to see its starter code, then download the zip and run dotnet build.
Build a
Code style
Tests
Folder tree 10 files
src/Acme.Hello/
tests/Acme.Hello.Tests/
src/Acme.Hello/Program.cs
The entry code. It reads the argument and prints the result; a missing or empty name goes to stderr with exit code 1. The modern style writes statements directly; the classic style puts them in static int Main.
Versions written in the files .NET 8 SDK · C# 12 · xUnit 2.5.3 · Microsoft.NET.Test.Sdk 17.8.0. NuGet packages are not in the zip. With the test project included, the first dotnet build downloads xUnit.
Where things go in a .NET solution
Path
What goes there
*.sln
The solution file. It groups several projects; dotnet build in the top folder builds every project listed here.
*.csproj
The project file (src/Name/Name.csproj): target framework (net8.0), app or library, packages and references to other projects. Every .cs file in the folder is included without listing it.
Program.cs
The entry code. The modern style writes code directly at the top of the file (top-level statements); the classic style uses static void Main.
tests/
The test project (tests/Name.Tests/). It references the main project, pulls in a test package such as xUnit, and runs with dotnet test.
appsettings.json
Settings for a web project. The development-only launch profile lives in Properties/launchSettings.json. Secrets go into user-secrets or environment variables, not here.
bin/ · obj/
Build output and intermediate files. They come back after a rebuild, so keep them out of git.
By convention the folder path matches the namespace: files in src/Acme.Hello/Services/ start with namespace Acme.Hello.Services;.
Splitting into layers
Separate the part that takes requests, the part that decides rules and the part that stores data. In a web API, the endpoint in Program.cs (MapGet) calls GreetingService, which calls IMessageRepository, never the other way round.
ASP.NET Core ships with a dependency injection container: register a type with builder.Services.AddSingleton<...>() and endpoints receive the right object as a parameter.
Endpoints take values from the request, pass them to a service and shape the response.
Services decide the rules and know nothing about HTTP, so they are easy to test.
Repositories sit behind an interface: a memory version now, a database version swapped in later.
Naming
Name namespaces and projects Company.Product, with a capital letter on each word (Acme.Hello). The test project adds .Tests.
Classes, methods and properties use PascalCase (MonthlyPay), locals and parameters camelCase (teamSize), and interfaces start with I (IBackup).
Avoid System, Console or Microsoft in your namespace. Inside Acme.Console, Console.WriteLine resolves to the namespace and fails.
One public type per file, with the file named after the type, keeps things easy to find (a habit, not a rule).
Four common design patterns and dependency injection
When to use each matters more than the name. All of this code was built and run on .NET 8.
Singleton · when there must be exactly one
For objects like settings that must exist once in the whole program. The constructor is private; you get the object only through Instance. Lazy<T> creates it once, on first use, safely across threads.
In ASP.NET Core, registering it with AddSingleton is easier to test than writing it by hand.
var a = AppSettings.Instance;var b = AppSettings.Instance;Console.WriteLine(ReferenceEquals(a, b));Console.WriteLine(a.Mode);sealedclassAppSettings{privatestaticreadonlyLazy<AppSettings> lazy = new(() => newAppSettings());publicstaticAppSettingsInstance => lazy.Value;publicstringMode { get; } = "production";privateAppSettings() { }}
Output
Trueproduction
Factory · deciding what to create in one place
A method that looks at the file extension and returns the right reader. A switch expression keeps it short, and unknown extensions raise an exception. Callers only know they get an IReader.
Pull a calculation with several variants, such as discounts, into an interface and pick one when creating the object. Here C# 12 primary constructors (class Checkout(IDiscount discount)) keep the wiring short. Simple rules can also be a Func<decimal, decimal> lambda.
C# builds this pattern into the language as event. Listeners sign up with +=, and the sender notifies all of them with one Restocked?.Invoke(...) line. With no listeners, ?. simply skips the call.
Dependency injection · receiving what you need through the constructor
GreetingService does not create its name store with new; it receives one in the constructor. The real program passes the memory store, and a test passes a fake that always returns the same name.
This is what builder.Services in ASP.NET Core does: it finds the registered implementation and passes it to the constructor (see the web API starter files).
Answer: 3. bin/ · obj/bin/ and obj/ hold build output that is recreated on every build. List them in .gitignore; whoever clones the repository runs dotnet build to get them back.
Question 2
Which way do the references go between the main project and the test project?
Show the answer
Answer: 1. The test project references the main project The test project lists the main project with a ProjectReference in its .csproj. The main project must not know about the tests, so xUnit never ships with your app.
Questions people ask
What is the difference between .sln and .csproj?
A .csproj configures one project (one app or dll); a .sln lists several projects as a group. The solution lets you build the main project and its tests together.
How do I build the zip I downloaded?
Run dotnet build in the top folder and dotnet test for the tests. Run the console app with dotnet run --project src/Name -- Mina. README.md in the zip lists every command.
Should I pick the modern or the classic style?
For new code, the modern style (top-level statements, file-scoped namespaces) is shorter and matches dotnet new. Pick classic to match older code or books. Both build to the same result.