In my previous tutorial, Getting Started with .NET Core, we learned how to build our first console app using .NET Core 2.0. We also learned how to package and deploy the application so it can be used by other users.
In this tutorial, we will learn how to build a class library using .NET standard 2.0. We will also see how to call a class library from a console app.
What A Class Library Is
Code reusability is one of the key features of a modern programming languages. Software developers often share same functionality among multiple applications to avoid the code redundancy and maintain code standards and quality.
Let’s take an example. Let’s say, Developer A (DevA) creates a Math class that provides functionality to add, multiply, square root, and other math functions.
Now, Developer B (DevB) and Developer C (DevC) also need same functionality. The first option is, either DevB and DevC also write similar code to implement same functionality, or they can use the same code that DevA has written already.
One option is, DevB and DevC copy code from A. That means, there will be three copies of the same code. What if DevA finds a bug and fixes in his/her code? Then, he has to give the changed code to DevB and DevC. What if there are thousands of developers who want DevA’s code?
The best and the simplest way to distribute and reuse what DevA can do is, build a class library and distribute it to DevB and DevC. One of the obvious examples is .NET Framework library. Microsoft has written many class libraries that have common functionality any developer can reuse.
A class library is a package of programs (code that has classes, types, interfaces, and other program elements) that is easily distributable, shareable, and reusable by other developers who want to implement the same functionality. Physically, a class library is a .dll (dynamic link library) file.
Class Library Frameworks
Currently, Visual Studio 2017 supports four types of class libraries project templates, .NET Framework, Universal class library, .NET Core, and .NET Standard. As you may guess, each template targets a specific framework with the exception of .NET Standard. A .NET Standard class library supports all platforms. See Figure 1.

Figure 1.
In this article, we will build a .NET Standard class library. If you’re not familiar with .NET Standard, I recommend watching this video: What is .NET Standard.
Note
The process of building a class library and consuming from a client app is same, no matter what framework do you choose to build your class library.
Building A .NET Standard Class Library
Now, you’ve installed Visual Studio 2017, let’s build your first Hello World! .NET Core application.
Step 1. Create a Solution and Client App
Create a .NET Core console app. If this is your first time working with .NET Core, read t Getting Started with .NET Core 2.0 and learn how to build your first .NET Core console app.
Note
We will use this app as a client app for the class library.
Step 2. Create a Class Library Project
Now, let’s create a class library project.
Right click on the solution and select Add -> New Project menu option. On the New Project dialog, select .NET Standard in the left side and Class Library (.NET Standard) template in the right side. See Figure 2.

Figure 2.
Give your project a name, select a folder where you want your project to be created, and click OK. This will create your first .NET Standard class library project.
I give my project name “MCMathLib”.
The Visual Studio project looks like Figure 3.

Figure 3.
Step 3. Add Class Library Functionality
The default program has a Class1.cs file that has the main code listed. This is where you will be writing more code. I changed the class name to MathClass and add a Calculate method. The Calculate method takes three arguments, two numbers, and one operation type. See Listing 1.
- using System;
- namespace McMathLib
- {
- public class MathClass
- {
- public MathClass() { }
- public double Calculate(double num1, double num2, string op)
- {
- double result = 0;
- switch (op)
- {
- case "Add":
- result = num1 + num2;
- break;
- case "Sub":
- result = num1 + num2;
- break;
- case "Mul":
- result = num1 * num2;
- break;
- case "Div":
- result = (double)num1 / num2;
- break;
- default:
- break;
- }
- return result;
- }
- }
- }







bheesham sharmaPosted Apr 29, 2019, 10:19 AM
Hi sir, you are giving example of standard .net library. if you go in .net core option there is also Class library(.net core). so please give the example on core. this is applicable only to non .net core
Alperen ARICIPosted Feb 10, 2019, 7:57 AM
I think amazing .
Rijwan AnsariPosted Sep 14, 2017, 6:48 AM
We have .Net framework library with some standard useful functions. We tried to use the library in .net core but getting error. Can we use .net framework library via .net standard in .net core?
Shakti Singh DulawatPosted Aug 31, 2017, 8:47 AM
Not sure the reason, but I was following this article and notice that some how project taking much time in the visual studio. ( I was checking on machine Core i7, 64 Bit, 8 GB RAM) Do you think we need to increase configurations for it? if the answer is yes then please suggest the best configuration.