Creating Class Library in Visual C#

Introduction

Steps to create and use a class library in ASP.NET/Visual Studio.

Create and use a class library

Step 1

Start Microsoft Visual Studio 

Step 2

On the Menu Bar, click File -> New Project.

Step 3

In the left list, click Windows under Visual C#.

Step 4

In the right list, click ClassLibrary 

Step 5

Change the Name to  SampleLibrary and click OK  

Step 6

Click OK.

Class Library 

When you click OK, C# will create a Namespace with the Name you've just used, and you'll be looking at the code window.

Class Library

Step 7

Change the class name to say "Algebra" and create any methods of your wish under Algebra template file opened. 

using System;  
using System.Collections.Generic;  
using System.Text;  
namespace SampleLibrary  
{  
    public class Algebra  
    {  
        public double Addition(double x, double y)  
        {  
            return x + y;  
        }  
        public double Subtraction(double x, double y)  
        {  
            return x - y;  
        }  
        public double Multiplication(double x, double y)  
        {  
            return x * y;  
        }  
        public double Division(double x, double y)  
        {  
            return x / y;  
        }  
    }  
}

Step 8

Build the Library: Since you would be creating a library and not an executable, to compile the project: 

  • On the main menu, you can click Build -> Build ProjectName 
  • In the Solution Explorer, you can right-click the name of the project and click Build 
  • In the Class View, you can right-click the name of the project and click Build 

Step 9

Create an ASP.NET WebApplication Project : After building the project, you can use it. You can use it in the same project where the library was built or you can use it in another project. If you are working in Microsoft Visual Studio, you can start by creating a new project. To use the library, you would have to reference the library. To do this: 

On the Menu bar, you would click Project -> Add Reference or In the Solution Explorer, you would right-click References and click Add Reference.

Add Reference

You can click the Browse tab, locate the folder where the library resides and select it.

Add reference

Class Library

Step 10

Call the Library class methods in your application : After selecting the library, you can click OK. You can then use the classes and methods of the library like you would use those of the .NET Framework. Here is an example:

using System;  
using SampleLibrary;  
public class Exercise  
{  
    static void Main()  
    {  
        Algebra alg = new Algebra();  
        double number1 = 100;  
        double number2 = 50;  
        double result = alg.Addition(number1, number2);  
        Console.Write(number1);  
        Console.Write(" + ");  
        Console.Write(number2);  
        Console.Write(" = ");  
        Console.WriteLine(result);  
    }  
}

Step 11

Build and run the project.

Summary

In this article, we learned about Creating Class Library in Visual C#.

Happy Learning...


Similar Articles