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 7
Change the class name to say "Algebra" and create any methods of your wish under Algebra template file opened.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. namespace SampleLibrary  
  5. {  
  6.     public class Algebra  
  7.     {  
  8.         public double Addition(double x, double y)  
  9.         {  
  10.             return x + y;  
  11.         }  
  12.         public double Subtraction(double x, double y)  
  13.         {  
  14.             return x - y;  
  15.         }  
  16.         public double Multiplication(double x, double y)  
  17.         {  
  18.             return x * y;  
  19.         }  
  20.         public double Division(double x, double y)  
  21.         {  
  22.             return x / y;  
  23.         }  
  24.     }  

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.
You can click the Browse tab, locate the folder where the library resides and select it.
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:
  1. using System;  
  2. using SampleLibrary;  
  3. public class Exercise  
  4. {  
  5.     static void Main()  
  6.     {  
  7.         Algebra alg = new Algebra();  
  8.         double number1 = 100;  
  9.         double number2 = 50;  
  10.         double result = alg.Addition(number1, number2);  
  11.         Console.Write(number1);  
  12.         Console.Write(" + ");  
  13.         Console.Write(number2);  
  14.         Console.Write(" = ");  
  15.         Console.WriteLine(result);  
  16.     }  

Step 11
Build and run the project.


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

Recommended Ebook

Working with Directories in C#

Download Now!
Similar Articles