Serving Industries Worldwide

Innovative Ways - Satisfied Clientele

Class Library in .NET Core


Listening is fun too.

Straighten your back and cherish with coffee - PLAY !

 
 

editor1

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

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.

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

  1. You can Right-click on the solution in the Solution Explorer and select Add -> New Project.
  2. 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.
  3. In the Configure your new project page, you can enter MathLibrary in the Project name box, and after you can select Next.
  4. 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

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.

  1. You can Right-click on the solution in Solution Explorer and select Add -> New Project.
  2. 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.
  3. Select the Console Application template and click on the Next.
  4. In the Configure your new project page, you can enter MathDemo in the Project name box. Click on the Next.
  5. 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

 

One Stop Solution for ASP.Net Web Development ?

Enquire Today.

 

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.

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.

editor1

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

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.

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

  1. You can Right-click on the solution in the Solution Explorer and select Add -> New Project.
  2. 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.
  3. In the Configure your new project page, you can enter MathLibrary in the Project name box, and after you can select Next.
  4. 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

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.

  1. You can Right-click on the solution in Solution Explorer and select Add -> New Project.
  2. 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.
  3. Select the Console Application template and click on the Next.
  4. In the Configure your new project page, you can enter MathDemo in the Project name box. Click on the Next.
  5. 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

 

One Stop Solution for ASP.Net Web Development ?

Enquire Today.

 

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.

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.