Project Structure

How C# files, namespaces, folders and the dotnet CLI fit together into a running app.

~3 min read updated Jul 19, 2026 Csharp
  • #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 your composer.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:

  1. Each file declares a namespace — a logical address for the types in it.
  2. Another file pulls that namespace in with using to make those types visible.
Services/LoginService.cs
namespace MyApp.Services;   // this file's address

public class LoginService { /* ... */ }
Program.cs
using MyApp.Services;       // now LoginService is visible here

var service = new LoginService(/* ... */);
There's no require/autoload step like PHP. The compiler sees every .cs file in the project automaticallyusing 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:

Program.cs
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.jsonMyApp.csproj
composer require xdotnet add package x
composer installdotnet restore (runs automatically on build)
php artisan serve / running the appdotnet run
PSR-4 autoload (folder → namespace)namespace convention (folder → namespace, but manual)
vendor/bin/ + obj/ (build output — gitignore these)

Where to go next

Related notes