Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
.NET Standard 2.0 Cookbook

You're reading from   .NET Standard 2.0 Cookbook Develop high quality, fast and portable applications by leveraging the power of .NET Standard Library

Arrow left icon
Product type Paperback
Published in May 2018
Publisher
ISBN-13 9781788834667
Length 394 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
 Ismail Ismail
Author Profile Icon Ismail
Ismail
Arrow right icon
View More author details
Toc

Table of Contents (18) Chapters Close

Title Page
Copyright and Credits
Packt Upsell
Contributors
Preface
1. Back to Basics FREE CHAPTER 2. Primitives, Collections, LINQ, and More 3. Working with Files 4. Functional Programming 5. XML and Data 6. Exploring Threading 7. Networking 8. To iOS with Xamarin 9. To Android with Xamarin 10. Let’s Fine-Tune Our Library 11. Packaging and Delivery 12. Deploying 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. 

You have been reading a chapter from
.NET Standard 2.0 Cookbook
Published in: May 2018
Publisher:
ISBN-13: 9781788834667
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime
Visually different images