How to Create A .NET Core Class Library

How to Building A .NET Core Class Library

Introduction

In the article, we’ll learn how to build our first console app using .NET Core 2.0. We also understand how to package and deploy the application so it can use by other users.

What is A Class Library

Code reusability is one of the key highlights of modern programming languages. Software developers frequently share
the same functionality among multiple applications to avoid the maintain code and code redundancy standards and quality.

Microsoft has composed many class libraries. that, have common functionality any developer can reuse.

A class library is a bundle of programs (code that has classes, interfaces, types, and other program elements). that is easily distributable, shareable, and reusable by other developers who need to execute a similar functionality. a class library is a .dll (dynamic link library) file.

Class Library Frameworks

Now, Visual Studio 2017 supports four types of class libraries project templates, .NET Framework, Universal class library, .NET Core, and .NET Standard. As you may figure, every format focuses on a particular structure except for .NET Standard. A .NET Standard class library bolsters all stages.

Currently, you’ve installed Visual Studio 2017, let’s build your first Hello World! .NET Core application.

Create a Solution and Client App

Create a .NET Core console app. If this is your first time working with .NET Core, then learn how to build your first .NET Core console app.

Create a Class Library Project

Now, let’s go and 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 template in the right side. See the following picture.

Class Library Project

So, let’s give a project name, select a folder where we want our project to be created, and click OK. This will make your first .NET Standard class library project.

here, we give the project name “MLib”.

The Visual Studio project looks like following picture.

 Visual Studio class file

Include Class Library Functionality

The default program has a Class1.cs file that has the main code recorded. This is the place you will compose more code. we change the class name to OperationsClass and add a Calculate method. The Calculate method takes three arguments, two numbers, and one operation type.

using System;    
    
namespace MLib    
{    
    public class OperationsClass    
    {    
        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;    
        }    
    }    
} 

Now, ensure your class library project is the default project in Visual Studio by Right clicking on MLib project and select Set as Startup Project in the menu.

Settings

You can set your library settings by right clicking on the library project and select the Settings menu item.

This will take you to the settings where you can set application, build, bundle and different settings. For instance, you can choose a framework version you want to target. You can change the namespace and assembly name. You can change marking information. See picture.

asp dotnet core application, build, bundle

Build the Project

Build your project by hitting F5 or Fn+F5 or by choosing Build menu item in Visual Studio. after the successful build looks like the following picture. The MLib.dll and also other files are created in the Release folder.

Build the Project

Consuming A Class Library

Now, let’s see how we can utilize the above class library functionality in the app.

Add Class Library Reference

To utilize a class library in your application, you should add a reference to the library to get to its functionality.

Right click on the project name of the console app in Solution Explorer and select Add option.

Class Library Reference

On the following picture, you will see the MLib is already available. That is because the class library is a piece of a similar solution.

 class library

If you don’t see the list, you can utilize the Browse alternative option and browse to the release folder where the .dll file was created.

We publish the release version of an assembly. Change the build choice in your Visual Studio from Debug to Release.

Import Namespace

Before you can utilize a class library and its classes, you should import the namespace by utilizing the accompanying code.

using MLib;

If your class library reference is an included effectively, as soon as you start composing “using M…”, you will see Visual Studio Intellisense will stack the namespace in the rundown.

class library namespace declaration

Call Functions

When the reference is added, all classes and public members of the library should be available in your client app. The code in Listing 2 makes an instance of OperationsClassClass and calls its Calculate method.

Create the class library object and method

MathClass math = new MathClass();  
double result = math.Calculate(num1, num2, op);

Now, let’s remove all code from our console app and type the following code. The console app reads two numbers and open operation and passes these three parameters to the OperationsClass. It Calculates method and displays the result returned from the class library.

namespace HelloWorld  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            Console.WriteLine("Hello World!");  
          
            // Read first number   
            Console.Write("Enter Number 1: ");  
            var var1 = Console.ReadLine();  
            // Convert string to int  
            int num1 = Convert.ToInt32(var1);  
            // Read second number  
            Console.Write("Enter Number 2: ");  
            var var2 = Console.ReadLine();  
            // Convert string to int  
            int num2 = Convert.ToInt32(var2);  
            // Read operation type - operator  
            Console.Write("Enter one Operator (Add/Sub/Mul/Div): ");              
            var op = Console.ReadLine();  
  
            // Create a class library object and call the Calculate method  
            MathClass math = new MathClass();  
            double result = math.Calculate(num1, num2, op);  
  
            Console.WriteLine("Result: {0}", result);  
              
            Console.WriteLine("Press any key to exit...");  
            Console.ReadKey(true);              
        }  
    }  
} 

Build and Run

Now, let’s build and run our application.

Build and Run project in c sharp

Leave a Reply

Your email address will not be published. Required fields are marked *