Skip to Content
Course content

77: NuGet Package Management

Click on the "Edit" button in the top corner of the screen to edit your slide content.

I've noticed a recurring theme with developers moving into C#: they often treat NuGet as if it's just a convenient "DLL downloader." They think of it like a digital filing cabinet where they go to grab a library, drop it into their project, and forget about it. But that's a dangerous way to look at it.

"NuGet is just a way to get DLLs" vs "NuGet manages a dependency graph"

If NuGet were just about downloading files, you'd just be copying .dll files into a /lib folder like we did in the early 2000s. The reality is that NuGet manages a dependency graph. When you install a package, you aren't just getting one piece of code; you're often getting a chain of other packages that the first one relies on.

Take a common example: let's say you install a high-level library for handling complex API requests. That library might depend on Newtonsoft.Json for serialization, which in turn might depend on a specific version of a system utility library. If you try to manually manage these, you'll eventually hit "Dependency Hell"—where Package A requires Version 1.0 of a library, but Package B requires Version 2.0. NuGet handles this resolution for you, deciding which version to actually ship in your build so the app doesn't crash at runtime with a MethodNotFoundException.

"Adding more packages is free" vs "The cost of transitive dependencies"

I've seen junior devs add a massive library like AutoMapper or Entity Framework just to use one single helper method. It feels "free" because the IDE handles the installation in seconds. But it isn't free. Every package you add increases your attack surface for security vulnerabilities and bloats your final binary size.

You need to look at the transitive dependencies. When you add one package, look at your .csproj file or the "Dependencies" node in your Solution Explorer. If adding one small utility brings in fifteen other libraries you didn't ask for, ask yourself: "Do I actually need this, or can I write these ten lines of code myself?" I usually lean toward writing it myself if the dependency chain is too deep. It keeps the project lean and makes updates much less painful.

"The GUI is the only way" vs "The power of the CLI and .csproj"

The NuGet Package Manager UI in Visual Studio is great for browsing, but if you want to be an efficient engineer, you need to get comfortable with the dotnet add package command and the .csproj file itself. The UI hides the "truth" of your project.

When you install a package via the CLI, you're modifying your project file. Take a look at a typical entry in your .csproj:

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

Because this is just XML, it lives in your version control (Git). This is why you should never check in your /bin or /obj folders. You don't check in the actual DLLs; you check in the instruction to download those DLLs. When your teammate pulls your code, NuGet reads that file and restores the exact versions needed. If you were just "downloading DLLs," you'd be bloating your Git repo with binary files, which is a cardinal sin in professional software engineering.




📋 Practical Task

Integrating Newtonsoft.Json to Parse a Local JSON Configuration

In this exercise, you will move beyond the GUI and use the CLI to manage a dependency for a specific task.

  • Create a new Console Application via the terminal using dotnet new console -n JsonParserProject.
  • Instead of using the Visual Studio NuGet Manager, use the terminal to install the Newtonsoft.Json package using the dotnet add package command.
  • Create a string variable containing a raw JSON object representing a user (e.g., "{"Name": "Alice", "Age": 30, "Role": "Engineer"}").
  • Create a C# class User with matching properties.
  • Use JsonConvert.DeserializeObject<User>() to turn that string into a typed C# object and print the user's Name to the console.
  • Open your .csproj file and verify that the PackageReference for Newtonsoft.Json is present and correctly versioned.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.