A class library and a console project

A solution grows into more than one project: a library with the logic and a console program that uses it, or a library and a web app. A project reference is a line in the csproj, and the SDK builds the library first. This lesson makes the smallest such pair and runs it.

The class library

dotnet new classlib writes a project with no entry point and one placeholder class, which the lesson replaces. The library declares a namespace of its own, and the calculator is static because it holds no state:

namespace Pricing; public static class Calculator { public static decimal Total(int quantity, decimal price) => quantity * price; }

dotnet new classlib -n Pricing -o Pricing

The template also writes a Class1.cs, and the lesson deletes it after adding Calculator.cs. The project file needs nothing: a class library with no package is the default SDK project with an empty PropertyGroup.

The console project

The console program imports the namespace of the library and calls it. It has no copy of the code:

using Pricing; Console.WriteLine($"total {Calculator.Total(3, 1.50m)}"); Console.WriteLine($"count {Calculator.Total(0, 9.99m)}");

dotnet new console -n Report -o Report

A program that calls a type of another project does not build on its own: the compiler reports error CS0246 for Calculator until the reference exists. That is why the course checker, which builds one file at a time, skips this listing; the pair was built and run as the two projects described here.

The project reference

dotnet add reference writes the reference into the csproj of the console project. The same line can be written by hand, and a project that references several libraries has several entries:

dotnet add Report/Report.csproj reference Pricing/Pricing.csproj

<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net10.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> <ItemGroup> <ProjectReference Include="..\Pricing\Pricing.csproj" /> </ItemGroup> </Project>

The include path is the one dotnet add reference wrote on this system, with the separator of the platform. A reference by path is a reference to a project; a reference to a compiled file is an AssemblyReference and does not rebuild the library.

Build and run

dotnet run --project builds the console project and the library it references, then runs the program:

dotnet run --project Report

total 4.50
count 0.00

Three times 1.50 is 4.50 and the zero quantity is 0.00, with the two decimals of the decimal type. The build wrote Pricing.dll next to the console assembly, and a change in the library is visible in the next run without any command to copy it. The lesson on publishing shows what a deployment does with the pair; the next lesson measures code instead of shipping it.

Exercises

  1. Add a second method to the library and call it from the console program.
  2. Remove the project reference and read the compiler error.
  3. Add a test console project that references both projects.
  4. Run dotnet build Report and find the library assembly in the output folder.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which command writes a project reference into a csproj?

    Answer

    dotnet add <project> reference <library>

    It writes a ProjectReference item; the lesson also shows the line it writes.

  2. What does dotnet new classlib write?

    Answer

    A project with no entry point and one placeholder class

    A library is compiled to a dll and cannot run by itself.

  3. What does the console project need to use the library type?

    Answer

    A ProjectReference to the library in its csproj

    Without the reference the compiler reports CS0246 for the type.

  4. What does the element in the csproj look like?

    Answer

    &lt;ProjectReference Include=&quot;..\Pricing\Pricing.csproj&quot; /&gt;

    The include is the path to the project file, and the SDK builds it first.

  5. What does dotnet run --project Report do?

    Answer

    Builds the project and the projects it references, then runs it

    The referenced library is built as part of the same build.

  6. What did the program print for 3 at 1.50?

    Answer

    total 4.50

    A decimal keeps the two places, as the measured line showed.

  7. Why does a library project have no OutputType Exe?

    Answer

    It is loaded by another program instead of running

    A library produces a dll for a host, and the SDK default is Library.

  8. Where does the library assembly end up for the console app?

    Answer

    Next to the console assembly in its output folder

    The build copies the referenced assemblies to the output of the app.

  9. What does a change in the library need before the app sees it?

    Answer

    A rebuild of the app, which dotnet run does

    The build detects the newer library and copies it again.

  10. Can one project reference several libraries?

    Answer

    Yes, one ProjectReference item per library

    The ItemGroup holds any number of references.

The code on this page was built with the .NET SDK 10.0.303 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev