---
title: "Creating and using dll libraries in C Sharp.Net"  
description: "Creating Class library  To create class library in C#, goto New Project in File Menu, a dialog box will appear, select Class Library icon in Visual C#"  
author: "Uttam Misra"  
published: 2010-07-30  
updated: 2020-03-04  
canonical: https://www.mindstick.com/articles/50/creating-and-using-dll-libraries-in-c-sharp-dot-net  
category: "c#"  
tags: ["c#"]  
reading_time: 3 minutes  

---

# Creating and using dll libraries in C Sharp.Net

Creating Class library\

To create class library in C#, goto New Project in File Menu, a dialog box will appear, select Class Library icon in Visual C#->Windows option, and change its name to your desired name, here I have used MyMethod.

![Creating and using dll libraries in C Sharp.Net](https://www.mindstick.com/mindstickarticle/ce035a1b-e20a-408a-aadf-e5011758786a/images/c4216b84-c6ba-4a14-ad3f-cef711d651d2.png)

\

\

\

\

\

\

\

\

\

Now create class and add methods. Here in the example, I have created two classes.\

##### Example

Add.cs

```
namespace MyMethods{//creating class AddClass     public class AddClass    {//Creating Method         public static long Add(long i, long j)         {            return (i + j);         }    }}
```

Subtract.cs

```
namespace MyMethods{     public class SubtractClass    {         public static long Subtract(long i, long j)         {            return (i - j);         }    }}
```

Note: we can create more than one method in a class ans more than one class in a namespace.

Now, once classes are created we need to build it so as to create its dll. To build the class library press ‘F6’ from key board or in ‘Build’ menu select ‘Build Solution’

To see the build dll check it in bin\debug folder of project.

##### Using class library in project

To use class library first of all we need to add it refrence in our project. To add its refrence right click on ‘Solution Explorer’ and select ‘Add Refrence’.

![Creating and using dll libraries in C Sharp.Net](https://www.mindstick.com/mindstickarticle/ce035a1b-e20a-408a-aadf-e5011758786a/images/75884d72-baab-4747-b7cb-b4ea8aad03d9.png)

\

\

\

\

\

\

\

A dialog box will apear. In that dialog box click ‘Browse’ and select the dll present in your bin\debug folder of class library project.\

![Creating and using dll libraries in C Sharp.Net](https://www.mindstick.com/mindstickarticle/ce035a1b-e20a-408a-aadf-e5011758786a/images/404d0408-a831-4b0e-911d-d48303708b9c.png)

\

\

\

\

\

\

\

\

\

Now add a namespace to your project. Here I have added\

using MyMethods;

As MyMethod is the Class Library I have created.

To demonstrate the usage of class library I have designed a claculator which has the capability to add or subtract two numbers.

![Creating and using dll libraries in C Sharp.Net](https://www.mindstick.com/mindstickarticle/ce035a1b-e20a-408a-aadf-e5011758786a/images/f86d3217-ea10-4ff1-85a7-184aba40ebf9.png)

## \

## \

##### Coding

```
  public partial class Form1 : Form    {         long a, b;         bool choice; //to check for which button press, add or subtract, true //for add and false for subtract.         public Form1()         {            InitializeComponent();         }           private void btnAdd_Click(object sender, EventArgs e)         {            //checking whether text box contains any value or not.                   if (txtVal.Text != "")            {      //converting value of text box to long                a = long.Parse(txtVal.Text);                txtVal.Text = "";            }            else                a = 0;     //setting choice to true as add button is clicked            choice = true;            txtVal.Focus();         }           private void btnSub_Click(object sender, EventArgs e)         {            if (txtVal.Text != "")            {                a = long.Parse(txtVal.Text);                txtVal.Text = "";            }            else                a = 0;     //seeting value of choice to true as subtract button is clicked            choice = false;            txtVal.Focus();         }           private void btnResult_Click(object sender, EventArgs e)         {     //assigning second value to text box to b            if (txtVal.Text != "")                b = long.Parse(txtVal.Text);            else                b = 0;     //checking whether choice is true or false.            if (choice ==  true)    //calling AddClass, Add method created in MyMtehod Library                txtVal.Text = AddClass.Add(a, b).ToString();            else    //calling SubtractClass, Subtract method created in MyMtehod Library                txtVal.Text = SubtractClass.Subtract(a, b).ToString();            txtVal.Focus();         }                       }
```

##### Screen shot

![Creating and using dll libraries in C Sharp.Net](https://www.mindstick.com/mindstickarticle/ce035a1b-e20a-408a-aadf-e5011758786a/images/512f3d4e-a23d-45d8-a195-483b3105b3fc.png)

\

\

\

\

\

\

---

Original Source: https://www.mindstick.com/articles/50/creating-and-using-dll-libraries-in-c-sharp-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
