NuGet packages contain compiled binary code that developers make available for other developers to use in their projects. For more information, see What is NuGet. This quickstart describes how to install the popular Newtonsoft.Json NuGet package into a .NET project by using the dotnet add package command.
You refer to installed packages in code with a using <namespace>
directive, where <namespace>
is often the package name. You can then use the package's API in your project.
dotnet
command-line tool. Starting in Visual Studio 2017, the dotnet CLI automatically installs with any .NET or .NET Core related workloads.You can install NuGet packages into a .NET project. For this walkthrough, create a simple .NET console project by using the dotnet CLI, as follows:
Create a folder named Nuget.Quickstart for the project.
Open a command prompt and switch to the new folder.
Create the project by using the following command:
dotnet new console
Use dotnet run
to test the app. You should see the output Hello, World!
.
Use the following command to install the Newtonsoft.json
package:
dotnet add package Newtonsoft.Json
After the command completes, open the Nuget.Quickstart.csproj file in Visual Studio to see the added NuGet package reference:
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>
In Visual Studio, open the Program.cs file and add the following line at the top of the file:
using Newtonsoft.Json;
Add the following code to replace the Console.WriteLine("Hello, World!");
statement:
namespace Nuget.Quickstart
{
public class Account
{
public string? Name { get; set; }
public string? Email { get; set; }
public DateTime DOB { get; set; }
}
internal class Program
{
static void Main(string[] args)
{
Account account = new Account
{
Name = "John Doe",
Email = "john@nuget.org",
DOB = new DateTime(1980, 2, 20, 0, 0, 0, DateTimeKind.Utc),
};
string json = JsonConvert.SerializeObject(account, Formatting.Indented);
Console.WriteLine(json);
}
}
}
Save the file, then build and run the app by using the dotnet run
command. The output is the JSON representation of the Account
object in the code:
{
"Name": "John Doe",
"Email": "john@nuget.org",
"DOB": "1980-02-20T00:00:00Z"
}
Congratulations on installing and using your first NuGet package!
Find more NuGet videos on Channel 9 and YouTube.
Next stepsLearn more about installing and using NuGet packages with the dotnet CLI:
Install and use packages by using the dotnet CLI
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4