Understanding the NuGet PackageReferences inside C# project files (.csproj)

Posted: (EET/GMT+2)

 

If you've worked with .NET Core or modern .NET SDK-style projects, you've seen the <PackageReference> entries inside your .csproj files. They replaced the older packages.config format and aim to make dependency management simpler.

A typical reference inside a C# project file looks similar to this:

<ItemGroup>
  <PackageReference Include="Newtonsoft.Json" Version="12.0.0" />
</ItemGroup>

Each entry declares:

  • Include: the package ID
  • Version: the version to restore
  • Optional settings such as PrivateAssets, ExcludeAssets and so on.

Package restore happens automatically during builds, and the restored packages are stored centrally in the user's profile (not inside each project folder), which saves disk space. (But, can get large without anybody noticing.)

One useful option is PrivateAssets="all", which keeps a package from flowing to projects that reference your library:

<PackageReference Include="StyleCop.Analyzers" Version="1.2.0"
                  PrivateAssets="all" />

This is especially helpful for build-time tools and analyzers.

The modern PackageReference format is compact, reliable, and easier to understand than the old package configuration files. It's one of those small but important improvements that has made working with .NET projects simpler.