Creating Class Library in Visual C#

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

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.


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


Step 6: 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 7: 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 8. 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..


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



Step 9: 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(" + ");
        Console.Write(" = ");

Step 10: Build and run the project...

Happy Learning...