We have created a .NET Standard 2.0-based class library in the previous recipe. In this recipe, we will be creating a Windows console-based application to use the library. The console-based application will be using the full .NET Framework under Windows, the current version of .NET Framework is 4.6.1.
Let's get ready to create the Windows console application to use the .NET Standard library we have built in the previous recipe. If you haven't followed the previous recipe, make sure you have completed it. We are going to use that solution and add the Windows console application to it. Open Visual Studio 2017 and open the solution we saved from the previous recipe. Click Build
| Build Solution
, or press Ctrl + Shift + B, and the solution should build successfully. Everything's ready for testing our class library.
- Open Visual Studio 2017.
- Now, open the solution from the previous recipe. Click
File
|Open
|Open Project/Solution
, or press Ctrl + Shift + O, and select theChapter1.StandardLib
solution. - Now, click on the
Chapter1.Library
solution label. ClickFile
|Add
|New Project
.
- In the
Add New Project
template dialog box, expand theVisual C#
node in the left-hand pane. SelectWindows Classic Desktop
and selectConsole App (.NET Framework)
from the right-hand pane.
- Now, in the
Name:
text box, typeChapter1.Standard.HelloConsole
and leave theLocation:
text box as it is.
- Click
OK
. - Now, the
Solution Explorer
(if not visible, press Ctrl + Alt + L) should look like this:
- Click
OK
. - In the
Solution Explorer
, double-click on theProgram.cs
filename under theChapter1.StandardLib.HelloConsole
project. - Scroll up till you reach the
using
directive part of the code and add the following code as the last line of that section:
using Chapter1.StandardLib;
- Now, in between the curly brackets of the
Main()
method, type the following code:
var myName = "Fiqri Ismail"; var helloMessage = new HelloUniverse(); Console.WriteLine(helloMessage.SayHello(myName)); Console.ReadLine();
- Hit F5 and see the code running:
- Press Enter to exit from the
Command Prompt
.
OK, let's dive behind the scenes of the stuff we just completed. From steps 1 to 7, we opened an existing project and added a new Windows console application. This project is a full .NET Framework project and its version is .NET Framework version 4.6.1. In steps 9 and 10, we added the reference to a .NET Standard class library project from the Windows console application. This is required to test the class library. Then, we can reference it and use it from the application, as we did in step 12.
In step 13, we created a variable to store the name (keep in mind, hardcoding is not a good practice). And then we have created an instance of the HelloUniverse
class that we created in the .NET Standard 2.0 class library. To display the output of the SayHello()
method to the console window, we have directly used the Console.WriteLine()
method. Finally, we waited until the user presses a key to exit from the console by using the Console.ReadLine()
method, or else the end user wouldn't be able to see any output in the console.