Book Image

.NET Standard 2.0 Cookbook

By : Fiqri Ismail
Book Image

.NET Standard 2.0 Cookbook

By: Fiqri Ismail

Overview of this book

The .NET Standard is a standard that represents a set of APIs that all .NET platforms have to implement, making it easy for developers to access and use one common library for their development needs. This book begins with a quick refresher, helping you understand the mechanics of the new standard and offering insight into how it works. You’ll explore the core library concepts, such as working with collections, configurations, I/O, security, and multithreading. You’ll explore the iOS and Android libraries of Xamarin and we’ll guide you through creating a .NET Standard 2.0 library, which you’ll use with both Android and iOS applications. In the final chapters, you’ll learn the various debugging and diagnostics tools to deliver quality libraries and create a NuGet package of the .NET Standard 2.0 library. By the end of this book, you’ll be able to expand your current workflow to various .NET flavors and have the essential skills to create a .NET Standard 2.0 library from scratch to package and deliver it to the world.
Table of Contents (18 chapters)
Title Page
Copyright and Credits
Packt Upsell
Contributors
Preface
Index

Creating a Windows console-based application to use the library


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. 

Getting ready

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. 

How to do it...

  1. Open Visual Studio 2017.
  2. Now, open the solution from the previous recipe. Click File | Open | Open Project/Solution, or press Ctrl + Shift + O, and select the Chapter1.StandardLib solution. 
  3. Now, click on the Chapter1.Library solution label. Click File | Add | New Project.
  1. In the Add New Project template dialog box, expand the Visual C# node in the left-hand pane. Select Windows Classic Desktop and select Console App (.NET Framework) from the right-hand pane. 
  1. Now, in the Name: text box, type Chapter1.Standard.HelloConsole and leave the Location: text box as it is. 
  1. Click OK.
  2. Now, the Solution Explorer (if not visible, press Ctrl + Alt + L) should look like this: 
  1. In the Chapter1.StandardLib.HelloConsole project tree, right-click on the References label and selectAdd Reference....  
  2. Under the Reference Manager dialog box, click on the Projects label in the left-hand pane. In the middle pane, check the Chapter1.StandardLib.HelloUniverse project. 
  1. Click OK.
  2. In the Solution Explorer, double-click on the Program.cs filename under the Chapter1.StandardLib.HelloConsole project. 
  3. 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;
  1. 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();
  1. Hit F5 and see the code running: 
  1. Press Enter to exit from the Command Prompt

How it works...

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.