×

iFour Logo

Class Library in .NET Core

Kapil Panchal - February 16, 2021

Listening is fun too.

Straighten your back and cherish with coffee - PLAY !

  • play
  • pause
  • pause
Class Library in .NET Core

The .Net class library defines the types and methods that are referred to by an application. If your class library targets .NET Standard 3.1, it can be called by any .NET implementation (including .NET Framework) that supports .NET Standard 3.1. If your class library targets .NET 5, it can be invoked by any application that targets .NET 5.

If you are creating a .Net core class library, you can distribute it as a NuGet package or as a bundled component with the application that uses it.

Prerequisites


First of all, install the Visual Studio 2019 version 16.8 or a later version with the .NET Core cross-platform development workload installed.

If you are using version 16.8 or a later version, The .NET 5.0 SDK will automatically be installed.

Looking to Hire .NET Developers For Your Business?

Create a Solution


We will create a blank solution to put the class library project in Visual Studio 2019. A Visual Studio solution serves as a container for single or multiple projects. You’ll include other related projects in the same solution.

There are the following steps to create the blank solution.

Open Visual Studio 2019.

In the start window, you can choose to create a new project.

In the Create a new project page, you can write the solution in the search box, and select the Blank Solution template, after you can click on the Next.

editor1

Figure: Blank Solution template

You can enter demoClassLibraryProject in the Project name box on the Configure your new project page.

Create a The NET Class Library Project


There are the following steps to add a new the .NET class library project named “MathLibrary” to the solution

  • You can Right-click on the solution in the Solution Explorer and select Add -> New Project.

  • In the Add a new project page, you can write the library in the search box. Select C# or Visual Basic from your language list, and select all platforms from your Platform list.

  • Select the .net Class Library template, and after you can select Next.

  • In the Configure your new project page, you can enter MathLibrary in the Project name box, and after you can select Next.

  • In the Additional information page, you can select .NET 5.0 (Current), and after choose to create.

Ensure that the library targets the correct version of .NET. You can Right-click on the class library project in Solution Explorer, and then choose Properties. The Target Framework text box displays that the project targets .NET 5.0.

When you’re using Visual Basic, you can clear the text in the Root namespace text box.

editor1

Figure: Property window of The .NET Class Library

For every project, Visual Basic automatically creates a namespace matching the name of the project.

using System;

namespace MathLibrary
{
    public class MathOperation
    {

        public double Perform(double n1, double n2, string operation)
        {
            double data = 0;
            switch(operation)
            {
                case "Addition":
                    data = n1 + n2;
                    break;
                case "Subtraction":
                    data = n1 - n2;
                    break;
                case "Multiplication":
                    data = n1 * n2;
                    break;
                case "Division":
                    data = (double)n1 / n2;
                    break;
                case "Modulo":
                    data = n1 % n2;
                    break;
                default:
                    Console.WriteLine("Plz you check your some operatiom");
                    break;
            }
            return data;
        }
    }
}

First of all, we will rename the class1.cs file and change the name of MathOperation after containing a method name of Perform. In the Perform method, we can pass two operands and one string data type respectively n1 n2 and “operation”. The user can select any of the basic math operations such as Addition, Subtraction, Multiplication, Division, and Modulo so we can pass the “operation” value in the switch case, so a user can perform any of the math operations. If the user selects the wrong operation so we can pass the “default” value.

In the Menu bar, you can select the Build -> Build Solution or press Ctrl + Shift + B to verify that the project compiles without error.

editor1

Figure: Output window of The .Net Class Library

One-Stop Solution for ASP.Net Web Development?

Add a Console App to the Solution


Create a console application that uses the .Net class library. The app will prompt the user to input the two numbers and select the operation such as Addition, Subtraction, Multiplication, Division, and Modulo. So, the user gets the result from any one of the above operations.

  • You can Right-click on the solution in Solution Explorer and select Add -> New Project.

  • In the Add a new project page, you can write console in the search box. Select C# or Visual Basic from your language list, and select All platforms from your Platform list.

  • Select the Console Application template and click on the Next.

  • In the Configure your new project page, you can enter MathDemo in the Project name box. Click on the Next.

  • In the Additional information page, you can select .NET 5.0 (current) in the Target Framework box. And click on the Create.

Insert a Class Library References


If the class library utilizes in your application, you must add a reference to the library to get to its functionality.

You can Right-click on the project name of the console app in Solution Explorer and select Add option and after select the Project Reference.

editor1

Figure: Add a Project Reference

editor1

Figure: Reference Manager Window

Example

Using MathLibrary;
				  

Import Namespace


Before you can use a class library and its classes, you need to import the namespace using the accompanying code.

Example

Create the .NET Class Library Object and Method


MathOperation mathOperation = new MathOperation();
double data = mathOperation.Perform(var1, var2, operation);
				

Example

First of all, you have to remove all the code from the Program.cs file and type the following code. The console application reads two numbers and opens the operation and passes through those three parameters to the MathOperation. It performs the method and shows the result returned from the .Net core class library.

Example

namespace MathDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Plz Enter Your Number 1:");
            var num1 = Console.ReadLine();
            int var1 = Convert.ToInt32(num1);

            Console.WriteLine("Plz Enter Your Number 2:");
            var num2 = Console.ReadLine();
            int var2 = Convert.ToInt32(num2);

            Console.WriteLine("Enter  a new operator (Addition/Subtraction/Multiplication/Division/Modulo)");
            var operation = Console.ReadLine();

            MathOperation mathOperation = new MathOperation();
            double data = mathOperation.Perform(var1, var2, operation);

            Console.WriteLine("The Result is : {0}", data);
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey(true);

        }
    }
}

Run the Project


You can Right-click on the MathDemo Project and select Set as Startup Project in the context menu in the Solution Explorer.

editor1

Figure: Set as Startup Project

Now, you can run the project.

Need to hire AngularJS developer for your business?

editor1

Figure: Output of The Dotnet Class Library

Conclusion


In this blog, we have discussed how to create a class library and how to use class library in the console application. After, we have configured the class library in a console application with a Math Operation example.

Class Library in .NET Core The .Net class library defines the types and methods that are referred to by an application. If your class library targets .NET Standard 3.1, it can be called by any .NET implementation (including .NET Framework) that supports .NET Standard 3.1. If your class library targets .NET 5, it can be invoked by any application that targets .NET 5. If you are creating a .Net core class library, you can distribute it as a NuGet package or as a bundled component with the application that uses it. Prerequisites First of all, install the Visual Studio 2019 version 16.8 or a later version with the .NET Core cross-platform development workload installed. If you are using version 16.8 or a later version, The .NET 5.0 SDK will automatically be installed. Looking to Hire .NET Developers For Your Business? CONNECT US Create a Solution We will create a blank solution to put the class library project in Visual Studio 2019. A Visual Studio solution serves as a container for single or multiple projects. You’ll include other related projects in the same solution. There are the following steps to create the blank solution. Open Visual Studio 2019. In the start window, you can choose to create a new project. In the Create a new project page, you can write the solution in the search box, and select the Blank Solution template, after you can click on the Next. Figure: Blank Solution template You can enter demoClassLibraryProject in the Project name box on the Configure your new project page. Create a The NET Class Library Project There are the following steps to add a new the .NET class library project named “MathLibrary” to the solution You can Right-click on the solution in the Solution Explorer and select Add -> New Project. In the Add a new project page, you can write the library in the search box. Select C# or Visual Basic from your language list, and select all platforms from your Platform list. Select the .net Class Library template, and after you can select Next. In the Configure your new project page, you can enter MathLibrary in the Project name box, and after you can select Next. In the Additional information page, you can select .NET 5.0 (Current), and after choose to create. Read More: Dependency Injection In Action Filters In Asp.Net Core Ensure that the library targets the correct version of .NET. You can Right-click on the class library project in Solution Explorer, and then choose Properties. The Target Framework text box displays that the project targets .NET 5.0. When you’re using Visual Basic, you can clear the text in the Root namespace text box. Figure: Property window of The .NET Class Library For every project, Visual Basic automatically creates a namespace matching the name of the project. using System; namespace MathLibrary { public class MathOperation { public double Perform(double n1, double n2, string operation) { double data = 0; switch(operation) { case "Addition": data = n1 + n2; break; case "Subtraction": data = n1 - n2; break; case "Multiplication": data = n1 * n2; break; case "Division": data = (double)n1 / n2; break; case "Modulo": data = n1 % n2; break; default: Console.WriteLine("Plz you check your some operatiom"); break; } return data; } } } First of all, we will rename the class1.cs file and change the name of MathOperation after containing a method name of Perform. In the Perform method, we can pass two operands and one string data type respectively n1 n2 and “operation”. The user can select any of the basic math operations such as Addition, Subtraction, Multiplication, Division, and Modulo so we can pass the “operation” value in the switch case, so a user can perform any of the math operations. If the user selects the wrong operation so we can pass the “default” value. In the Menu bar, you can select the Build -> Build Solution or press Ctrl + Shift + B to verify that the project compiles without error. Figure: Output window of The .Net Class Library One-Stop Solution for ASP.Net Web Development? Enquire Today. Add a Console App to the Solution Create a console application that uses the .Net class library. The app will prompt the user to input the two numbers and select the operation such as Addition, Subtraction, Multiplication, Division, and Modulo. So, the user gets the result from any one of the above operations. You can Right-click on the solution in Solution Explorer and select Add -> New Project. In the Add a new project page, you can write console in the search box. Select C# or Visual Basic from your language list, and select All platforms from your Platform list. Select the Console Application template and click on the Next. In the Configure your new project page, you can enter MathDemo in the Project name box. Click on the Next. In the Additional information page, you can select .NET 5.0 (current) in the Target Framework box. And click on the Create. Insert a Class Library References If the class library utilizes in your application, you must add a reference to the library to get to its functionality. You can Right-click on the project name of the console app in Solution Explorer and select Add option and after select the Project Reference. Figure: Add a Project Reference Figure: Reference Manager Window Example Using MathLibrary; Import Namespace Before you can use a class library and its classes, you need to import the namespace using the accompanying code. Example Create the .NET Class Library Object and Method MathOperation mathOperation = new MathOperation(); double data = mathOperation.Perform(var1, var2, operation); Example First of all, you have to remove all the code from the Program.cs file and type the following code. The console application reads two numbers and opens the operation and passes through those three parameters to the MathOperation. It performs the method and shows the result returned from the .Net core class library. Example namespace MathDemo { class Program { static void Main(string[] args) { Console.WriteLine("Plz Enter Your Number 1:"); var num1 = Console.ReadLine(); int var1 = Convert.ToInt32(num1); Console.WriteLine("Plz Enter Your Number 2:"); var num2 = Console.ReadLine(); int var2 = Convert.ToInt32(num2); Console.WriteLine("Enter a new operator (Addition/Subtraction/Multiplication/Division/Modulo)"); var operation = Console.ReadLine(); MathOperation mathOperation = new MathOperation(); double data = mathOperation.Perform(var1, var2, operation); Console.WriteLine("The Result is : {0}", data); Console.WriteLine("Press any key to exit..."); Console.ReadKey(true); } } } Run the Project You can Right-click on the MathDemo Project and select Set as Startup Project in the context menu in the Solution Explorer. Figure: Set as Startup Project Now, you can run the project. Need to hire AngularJS developer for your business? Connect us now Figure: Output of The Dotnet Class Library Conclusion In this blog, we have discussed how to create a class library and how to use class library in the console application. After, we have configured the class library in a console application with a Math Operation example.
Kapil Panchal

Kapil Panchal

A passionate Technical writer and an SEO freak working as a Content Development Manager at iFour Technolab, USA. With extensive experience in IT, Services, and Product sectors, I relish writing about technology and love sharing exceptional insights on various platforms. I believe in constant learning and am passionate about being better every day.

Build Your Agile Team

Enter your e-mail address Please enter valid e-mail

Categories

Ensure your sustainable growth with our team

Talk to our experts
Sustainable
Sustainable
 
Blog Our insights
19 Power BI Case Studies That Showcase Real-World Success
19 Power BI Case Studies That Showcase Real-World Success

Customer analysis is a big deal for all kinds of industries. It's essential to learn about their patterns and behaviors over time to spot chances for conversion or keeping them around....

Location Intelligence Use Cases, and Benefits
Location Intelligence Use Cases, and Benefits

You must be wondering what exactly is Location Intelligence. To put it simply, it is basically a way of deriving actionable insights from geographic data and spatial relationships...

13 Ways Power Apps Simplifies eDiscovery
13 Ways Power Apps Simplifies eDiscovery

E-Discovery is a crucial process for legal research enabling lawyers to find the digital evidence they need. It involves finding, collecting, and filtering e-data related to their...