NuGet

Create, publish and consume a NuGet package using C#.


NuGet project logo
 by NuGet project team is licensed under CC

Recursion

When a function is called, the computer must "remember" the place it was called from... so that it can return to that location with the result once the call is complete. Typically, this information is saved on the call stack... For tail calls, there is no need to remember the caller...

https://en.wikipedia.org/wiki/Tail_call

Some interpreters (and compilers) eliminate the stack frame creation and destruction work when they recognize tail recursion.

C#

Adding a "HaddleyOffice365.dotnet-factorial" package to NuGet.org.

NuGet is the package manager for .NET.

Start by creating a dotnet-factorial repository in the https://github.com/HaddleyOffice365 profile.

New repository will be named dotnet-factorial

Publish to GitHub

Open in Visual Studio Code

Create a .NET solution and the .NET projects

Create a dotnet solution

$ dotnet new sln

Create a class library project and add the new class library project to the solution

$ dotnet new classlib -o dotnet-factorial
$ dotnet sln add ./dotnet-factorial/dotnet-factorial.csproj


Create a test project and add the new test project to the solution

$ dotnet new mstest -o unittests
$ dotnet sln add ./unittests/unittests.csproj


At a reference from the unit tests project to the factorial project.

$ dotnet add  ./unittests/unittests.csproj reference ./dotnet-factorial/dotnet-factorial.csproj

Add a .gitignore file to the unit tests project directory and to the factorial project directory.

$ cd dotnet-factorial
$ dotnet new gitignore
$ cd ../unittests
$ dotnet new gitignore
$ cd ..


new solution, new classlib project and new mstest project

Rename UnitTest1, copy and rename Class1

Rename the generated UnitTest1 class to "UnitTests"

Copy Class1 class and rename to "recursivefunctions.cs"
Rename the generated Class1 class to "iterativefunctions"

Update the file contents:

UnitTests.cs
iterativefunctions.cs
recursivefunctions.cs

dotnet test

use "dotnet test" to run the unit test locally on the development machine.

$ dotnet test

dotnet test

Commit to main

Commit updates to the repository.

Commit to main branch

Push origin

GitHub actions

A GitHub action will ensure that testing is performed automatically.

Add the ".NET By GitHub Actions" workflow

dotnet.yml

GitHub action running

GitHub action finished

GitHub action details

PackageLicenseExpression, PackageId and Version

Add PackageLicenseExpression, PackageId and Version tags to the Functions.csproj file

dotnet-factorial.csproj

dotnet-factorial.csproj updates

dotnet pack

use "dotnet pack" to create .nupkg file

$ dotnet pack

dotnet pack

Listing the NuGet package

Upload the nupkg file to nuget.org. 

+ Add new

Browse...

Choose for Upload

Verify

Submit

Successful upload. Status Validating

Status Listed

Consuming the NuGet Package

$ dotnet new console
$ dotnet new gitignore
$ dotnet add package HaddleyOffice365.dotnet-factorial --version 1.0.0

dotnet add package HaddleyOffice365.dotnet-factorial --version 1.0.0

Program.cs

dotnet run

$ dotnet run

dotnet run