DEV Community

Harshal Suthar
Harshal Suthar

Posted on • Originally published at ifourtechnolab.com

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.

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?

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.

Image description

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.

Image description

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;
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

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.

Image description

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.

Image description

Image description

Example

Using MathLibrary;
Enter fullscreen mode Exit fullscreen mode

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);

Enter fullscreen mode Exit fullscreen mode

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);

        }
    }
}

Enter fullscreen mode Exit fullscreen mode

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.

Image description

Now, you can run the project.

Need to hire AngularJS developer for your business?

Image description

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.

Top comments (0)