Reader Level:
ARTICLE

Creating Class Library in Visual C#

Posted by Suresh Paldia Articles | C# Language November 15, 2010
In this article we will see steps to create and use a class library in ASP.NET/Visual Studio.
  • 1
  • 0
  • 53980

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.

1.gif 

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

2.gif

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..

3.gif

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

4.gif

5.gif

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(number1);
        Console.Write(" + ");
        Console.Write(number2);
        Console.Write(" = ");
        Console.WriteLine(result);
    }
}

Step 10: Build and run the project...

Happy Learning...

COMMENT USING

Trending up