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 C# class library 


In this recipe, we are going to build a simple C# class library. This library will have a simple public method that takes a parameter and returns a string. Also, we will be creating a blank Visual Studio solution and adding the library project. This solution will be used in later recipes. 

Getting ready

Make sure you have installed a flavor of Visual Studio 2017 and its latest updates. At the time of writing, the latest Visual Studio 2017 version is 15.3.5.

How to do it...

  1. Open Visual Studio 2017.
  2. Click File | New | Project and, in the New Project template dialog box, select Visual Studio Solutions under the Other Project Types node in the left-hand pane, and select Blank Solution in the right-hand pane:
  1. In the Name: textbox, type a name for your application. In this case, type Chapter1.Library. Select a preferred location under the Location: drop-down list or click the Browse... button and select a location. Leave the defaults as they are:
  1. Now you have a blank solution. Let's add a C# class library project to the solution. Click Project | Add NewItem... or you can right-click on the Chapter1.Library solution label in the Solution Explorer, and select Add | New Project....
  2. In the Add New Project template dialog box, select Visual C# in the left side, pane and select Class Library (.NET Framework) in the right-hand pane:
  1. In the Name: textbox, type a name for your class library. In this case, type Chapter1.Library.HelloLib as the name of the project. Leave the current location under the Location: drop-down list and click OK to create the project:
  1. Now we have a brand new .NET Framework-based class library. In the Solution Explorer (press Ctrl + Alt + L if you don't see the Solution Explorer), the default structure should look like this: 
  1. Now we have a default template for a class library project. Let's rename Class1.cs to something more meaningful. Rename it HelloWorld.cs. You can simply soft click on the label of the file in the Solution Explorer and type the new name (or click on the filename label and press F2). Click Yes in the confirmation box to confirm the renaming. 
  2. Type the following code snippet in the HelloWorld class body: 
      public string SayHello(string name) 
      { 
          return $"Hello {name}, congratulations !!!, 
          this message is from the class library you created."; 
      }
  1. Let's build our code to check that everything is fine. Click Build | Build Solution, or press Ctrl + Shift + B, and the solution should build successfully. Let's test our class library in the next recipe. 
  2. Click File | Save All, or press Ctrl + Shift + S, to save the solution and the class library project. 

How it works...

Let's see what we have done so far in this recipe and how it works. In steps 1 to 3, you have created a blank solution. Blank solutions are a very good starting point for any size of project. It gives you a whole new solution to start with. Later on, you can add more bits and pieces to your solution. Even though this is a simple introduction to class libraries, it is good practice to stick with proper naming conventions. It's not a must, but good practice. As you can see, we have given a name Chapter1.Library, so the name is meaningful and it says what our solution is about. 

In the next steps, from 4 to 8, we have added a class library project to our blank solution. Now you have an idea how a solution will grow over time, from start to end. The template we have chosen is a full .NET Framework class library. We renamed the default Class1.cs template provided by Visual Studio. It's good practice to give a meaningful name to classes and the files we work with. 

In steps 9 and 10, we added code to our class and checked all the syntax was correct by building the solution. It is also good practice to check for typos and other errors in syntax once in a while.

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 £13.99/month. Cancel anytime
Visually different images