Create a sample .NET project
To set up a .NET project to work with dependencies, we’ll use Visual Studio Code. Visual Studio Code includes an integrated terminal, which makes creating a new project easy. If you don’t want to use another code editor, you can run the commands in this module in a terminal.
- In Visual Studio Code, select File > Open Folder.
- Create a new folder named DotNetDependencies in the location of your choice, and then select Select Folder.
- Open the integrated terminal from Visual Studio Code by selecting View > Terminal from the main menu.
- In the terminal window, copy and paste the following command.
.NET CLI
dotnet new console -f net6.0
This command creates a Program.cs file in your folder with a basic “Hello World” program already written, along with a C# project file named DotNetDependencies.csproj.
You should now have access to these files.
-
-| obj -| DotNetDependencies.csproj -| Program.cs - In the terminal window, copy and paste the following command to run the “Hello World” program.
.NET CLI
dotnet run
Add a NuGet package by using the .NET Core tool
- Open Program.cs. It should look like this:
C#
-
Console.WriteLine("Hello, World!");The preceding function is run at the start of the application and outputs a string to the console. Let’s add Humanizer and manipulate data and write it to the console.
- Install the Humanizer library by running the following command:
.NET CLI
dotnet add package Humanizer --version 2.7.9
Open the DotNetDependencies.csproj file and find the ItemGroup section. You should now have an entry that looks like this one:
-
<ItemGroup> <PackageReference Include="Humanizer" Version="2.7.9" /> </ItemGroup> - Add the following content at the top of the Program.cs file to initialize Humanizer:
C#
using Humanizer;
Your Program.cs should now look like this:
-
using Humanizer; Console.WriteLine("Hello, World!"); - Add the following content to the Program.cs file to the bottom of file under the
Console.WriteLine("Hello, World!");:C# -
static void HumanizeQuantities() { Console.WriteLine("case".ToQuantity(0)); Console.WriteLine("case".ToQuantity(1)); Console.WriteLine("case".ToQuantity(5)); } static void HumanizeDates() { Console.WriteLine(DateTime.UtcNow.AddHours(-24).Humanize()); Console.WriteLine(DateTime.UtcNow.AddHours(-2).Humanize()); Console.WriteLine(TimeSpan.FromDays(1).Humanize()); Console.WriteLine(TimeSpan.FromDays(16).Humanize()); } - Replace the
Console.WriteLine("Hello, World!");with the following code:C# -
Console.WriteLine("Quantities:"); HumanizeQuantities(); Console.WriteLine("\nDate/Time Manipulation:"); HumanizeDates(); - Save the file (File > Save or CTRL + S). Run the application by running the following command in the terminal:
.NET CLI
dotnet run
You should see the following output.
Quantities:
0 cases
1 case
5 cases
Date/Time Manipulation:
yesterday
2 hours ago
1 day
2 weeks
Leave A Comment?