SkillPilotAI

Solution, projects and NuGet

Real .NET apps are organised with namespaces (unique full names), projects (each builds a dll), solutions (groups of projects) and NuGet packages (ready-made libraries). Getting this structure right keeps builds clean and versions consistent as the app grows.

19.1 The problem: same class name, no namespace

C#
// Billing/Order.cspublic class Order{    public decimal Amount { get; set; }} // Delivery/Order.cspublic class Order{    public string City { get; set; } = "";}
Output
$ dotnet buildDelivery\Order.cs(1,14): error CS0101:  The namespace '<global namespace>' already  contains a definition for 'Order'Build FAILED.
  • For C# a folder only holds files; a class's full name comes from its namespace.
  • No namespace declared -> both classes land in the global namespace, where a name can exist only once.
  • Every class needs a unique full name: that is what a namespace gives.

19.2 File-scoped namespace

  • namespace ShopApp.Billing; (ends with a semicolon) applies to the whole file, no extra braces or indentation.
  • Convention: the namespace follows the folder structure.
C#
// Billing/Order.csnamespace ShopApp.Billing; public class Order{    public decimal Amount { get; set; }} // Delivery/Order.csnamespace ShopApp.Delivery; public class Order{    public string City { get; set; } = "";}

19.3 using, fully qualified names and global using

  • using ShopApp.Billing; lets you write just Order for the billing Order.
  • For the other one write the fully qualified name: ShopApp.Delivery.Order.
C#
using ShopApp.Billing; var bill = new Order { Amount = 499 };var parcel = new ShopApp.Delivery.Order{    City = "Pune"}; Console.WriteLine(bill.GetType().FullName);Console.WriteLine(parcel.GetType().FullName);
Output
$ dotnet runShopApp.Billing.OrderShopApp.Delivery.Order
  • global using ShopApp.Billing; written once applies to every file in the project.
  • ImplicitUsings does the same for common namespaces; see the generated file in obj.
C#
// GlobalUsings.csglobal using ShopApp.Billing; // Program.csvar bill = new Order { Amount = 499 };Console.WriteLine($"Bill: {bill.Amount}");
Output
$ cat obj/Debug/net10.0/ShopApp.GlobalUsings.g.cs// <auto-generated/>global using System;global using System.Collections.Generic;global using System.IO;global using System.Linq;...

19.4 Solution, class library and project reference

  • A solution groups projects; in .NET 10 dotnet new sln creates the .slnx format.
  • A class library builds a dll for shared code and does not run on its own.
  • A project reference lets App use the public classes of Core.
Terminal
dotnet new sln -n Shopdotnet new classlib -n Shop.Coredotnet new console -n Shop.Appdotnet sln add Shop.Core Shop.Appdotnet add Shop.App reference Shop.Core
Output
$ dotnet add Shop.App reference Shop.CoreReference `..\Shop.Core\Shop.Core.csproj` added to the project.
XML
<!-- Shop.slnx --><Solution>  <Project Path="Shop.App/Shop.App.csproj" />  <Project Path="Shop.Core/Shop.Core.csproj" /></Solution> <!-- Shop.App/Shop.App.csproj --><ItemGroup>  <ProjectReference    Include="..\Shop.Core\Shop.Core.csproj" /></ItemGroup>
Output
$ dotnet build  Shop.Core -> Shop.Core\bin\Debug\net10.0\Shop.Core.dll  Shop.App -> Shop.App\bin\Debug\net10.0\Shop.App.dllBuild succeeded.
  • .slnx is simple XML listing project paths: easier to read and merge in Git than the old .sln.
  • Core builds first, then App, because App depends on Core.

19.5 Adding a NuGet package

  • NuGet is the .NET package manager; nuget.org hosts hundreds of thousands of libraries.
  • dotnet add package Humanizer.Core downloads it and adds a PackageReference to the csproj.
Terminal
dotnet add package Humanizer.Core
C#
using Humanizer;using Shop.Core.Billing; var total = Gst.Add(1000);Console.WriteLine($"Total: {total}"); Console.WriteLine("order_shipped".Humanize());Console.WriteLine(    TimeSpan.FromMinutes(90).Humanize(2));
Output
$ dotnet run --project Shop.AppTotal: 1180.00order shipped1 hour, 30 minutes
Where a package comes from
  1. 1.csproj has <PackageReference Include="Humanizer.Core" />

  2. 2.dotnet restore downloads it from nuget.org

  3. 3.Saved once in the machine's global packages folder

  4. 4.Next project reuses the cached copy

  5. 5.dotnet build compiles the package dll with your code

Download once, reuse from the cache

19.6 Checkpoint: a circular reference

Terminal
dotnet add Shop.Core reference Shop.Appdotnet build
Output
$ dotnet add Shop.Core reference Shop.AppReference `..\Shop.App\Shop.App.csproj` added to the project.$ dotnet builderror MSB4006: There is a circular dependency  in the target dependency graph ...Build FAILED.
  • The add command succeeds, but the build fails with MSB4006.
  • Keep references in one direction; shared code goes in the lower library.

19.7 Directory.Build.props and Central Package Management

  • Directory.Build.props in the solution folder is auto-imported by MSBuild into every project.
  • Put shared settings there (TargetFramework, Nullable, TreatWarningsAsErrors); each csproj keeps only its extras.
XML
<!-- Directory.Build.props --><Project>  <PropertyGroup>    <TargetFramework>net10.0</TargetFramework>    <ImplicitUsings>enable</ImplicitUsings>    <Nullable>enable</Nullable>    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>  </PropertyGroup></Project> <!-- Shop.App.csproj: only extras --><PropertyGroup>  <OutputType>Exe</OutputType></PropertyGroup>
  • Different package versions in each csproj (Core 3.0.10, App 2.14.1) -> restore fails with NU1605 (package downgrade).
  • Central Package Management: all versions in Directory.Packages.props, csproj files reference packages without a version.
XML
<!-- Directory.Packages.props --><Project>  <PropertyGroup>    <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>  </PropertyGroup>  <ItemGroup>    <PackageVersion Include="Humanizer.Core"                    Version="3.0.10" />  </ItemGroup></Project> <!-- every csproj, no version --><PackageReference Include="Humanizer.Core" />

19.8 Comparison: project reference vs package reference

PointProjectReferencePackageReference
Points toAnother project in the same solutionA package on NuGet
BuiltFrom source, together with yoursAlready built and versioned
Added withdotnet add referencedotnet add package
Arrives onBuildRestore (cached in global packages folder)

19.9 Common mistakes

  • Thinking namespace = project: a namespace is a logical group of names; a project builds one dll (assembly) that can hold many namespaces.
  • Thinking using adds a library: using only shortens names; the reference is added in the csproj.
  • Circular project references (MSB4006).
  • A different package version in every csproj (NU1605); use CPM.

19.10 5 rules for projects

  1. File-scoped namespace matching the folder path.
  2. Shared code in a class library, references in one direction.
  3. All projects in one .slnx solution.
  4. Directory.Build.props for settings, CPM for versions.
  5. Only trusted, maintained packages; check the licence.

19.11 Interview tips

  • Project reference = another project in the solution, built from source with yours; package reference = a built, versioned NuGet package downloaded on restore.
  • Namespace = logical name grouping; assembly (dll) = the build output of a project.
  • Large solutions use Directory.Build.props and Central Package Management so every project stays on one version.
  • Keep the dependency graph one-directional (no circular references).