Project Structure
How C# files, namespaces, folders and the dotnet CLI fit together into a running app.
#csharp #dotnet #tooling #architecture
The glue: how the files from the other notes become one program you can run.
Create and run a project
The dotnet CLI is your composer + artisan combined:
dotnet new console -n MyApp # create a console app in ./MyApp
cd MyApp
dotnet run # build + run
dotnet build # build only
dotnet add package Newtonsoft.Json # add a NuGet package (like composer require)
dotnet new console gives you two important files:
MyApp.csproj— the project manifest. Target framework, package references, settings. This is yourcomposer.json.Program.cs— the entry point. Code here runs when the app starts.
A typical layout
Folders are for organisation; they don't create structure by themselves the way Laravel's autoloader maps directories. What actually connects files is the namespace, which by convention mirrors the folder path:
MyApp/
├─ MyApp.csproj
├─ Program.cs // entry point
├─ Models/
│ └─ User.cs // namespace MyApp.Models
├─ Services/
│ └─ LoginService.cs // namespace MyApp.Services
└─ Helpers/
└─ TextHelper.cs // namespace MyApp.Helpers
Namespaces & using — how files see each other
This is the one genuinely new concept vs PHP. Two steps:
- Each file declares a namespace — a logical address for the types in it.
- Another file pulls that namespace in with
usingto make those types visible.
namespace MyApp.Services; // this file's address
public class LoginService { /* ... */ }
using MyApp.Services; // now LoginService is visible here
var service = new LoginService(/* ... */);
require/autoload step like PHP. The compiler sees every .cs
file in the project automatically — using doesn't load the file, it just
saves you typing the full name (MyApp.Services.LoginService). Folder names and
namespaces don't have to match, but keeping them aligned is the convention and
makes files easy to find.The entry point
Modern C# (.NET 6+) lets Program.cs be plain top-level statements — no class
or Main boilerplate needed:
using MyApp.Models;
using MyApp.Services;
var users = new List<User>
{
new User { Username = "ada", Password = "secret" }
};
var loginService = new LoginService(users);
Console.Write("Username: ");
var username = Console.ReadLine() ?? "";
Console.Write("Password: ");
var password = Console.ReadLine() ?? "";
var user = loginService.Login(username, password);
Console.WriteLine(user is not null ? $"Welcome, {user.Username}!" : "Invalid credentials.");
Laravel → .NET tooling cheat
| Laravel / PHP | .NET |
|---|---|
composer.json | MyApp.csproj |
composer require x | dotnet add package x |
composer install | dotnet restore (runs automatically on build) |
php artisan serve / running the app | dotnet run |
| PSR-4 autoload (folder → namespace) | namespace convention (folder → namespace, but manual) |
vendor/ | bin/ + obj/ (build output — gitignore these) |
Where to go next
- Solidify the language → C# basics
- Model your data → Classes & models
- Organise behaviour → Services & helpers
Services & Helpers
Put code in another file — a service or a static helper — and call it from anywhere, the C# way.
Git
Version control — everyday commands and getting out of trouble.