×

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

A class library defines the types and methods which 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.

 

Table of Content

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 Class Library Project


There are the following steps to add a new .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 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 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 Class Library

One Stop Solution for ASP.Net Web Development ?

Add a Console App to the Solution


Create a console application that uses the 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 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 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 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 A class library defines the types and methods which 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.   Table of Content 1. Prerequisites 2. Create a Solution 3. Create a Class Library Project 4. Add a Console App to the Solution 5. Insert a Class Library References 6. Import Namespace 7. Create the Class Library Object and Method 8. Run the Project 9. Conclusion If you are creating a 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 Class Library Project There are the following steps to add a new .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 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 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 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 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 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 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 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.

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

React 19 For Business: Latest Features and Updates
React 19 For Business: Latest Features and Updates

When I first started exploring React.js for our company’s project, I felt a bit lost. It was like trying to solve a big puzzle without all the pieces! But as I kept learning and trying them practically, I discovered some really cool features that blew my mind making everything seamlessly easier.

MySQL vs Azure SQL Database: Cost, Security, and Compatibility Considerations
MySQL vs Azure SQL Database: Cost, Security, and Compatibility Considerations

This blog is a continuation of MySQL vs Azure SQL Database – Part 1 , where we compared MySQL and Azure SQL databases. We learned how important it is to identify and evaluate client...

Is It Worth Using Azure With Power Platforms For Financial Business?
Is It Worth Using Azure With Power Platforms For Financial Business?

The era of traditional software development is fading; Azure Cloud and Power Platform services are taking charge to run businesses of the new age. When it comes to Financial business,...