Home > DeveloperSection > Articles > Creating C# Class Library (DLL) Using Visual Studio .NET

Creating C# Class Library (DLL) Using Visual Studio .NET


.NET .NET 
Ratings:
0 Comment(s)
 45298  View(s)
Rate this:

Creating C# Class Library (DLL) Using Visual Studio .NET


Creating C# Class Library in Visual Studio .NET is an easy way. Here I have describe, How to create DLL in Visual Studio and How to access this DLL in our project. Here I used Visual Studio 2010.

Steps for Creating DLL

Step 1:- File->New->Project->Visual C# Projects->Class Library. Select your project name and appropriate directory click OK

Creating C# Class Library (DLL) Using Visual Studio .NET

After Clicking on button ‘OK’, solution explorer adds one C# class ‘Class1.cs’. In this class we can write our code.

Creating C# Class Library (DLL) Using Visual Studio .NET

When we double click on Class1.cs, we see a namespace CreatingDLL. We will be use this namespace in our project to access this class library.

 

Creating C# Class Library (DLL) Using Visual Studio .NET

 

Step 2:- Within Class1.cs we create a method named ‘sum’ that takes two integers value and return sum to witch method passed numbers.

 

using System;

namespace CreatingDLL

{

    public class Class1

    {

        /// <summary>

        /// sum is method that take two integer value and return that sum

        /// </summary>

        /// <param name="x"></param>

        /// <param name="y"></param>

        /// <returns></returns>

        public int sum(int x, int y)

        {

            return x + y;

        }

    }

}

 

Step 3:- Now build the Application and see bin\debug directory of our project. ‘CreatingDLL.dll’ is created.

Now we create another application and take this DLL (CreatingDLL.dll) reference for accessing DLL’s method.

Steps for Accessing Created DLL

Step 4:- File->New->Project->Visual C# Projects->Windows Form Application.  

Step 5:- Designed windows form as bellow figure.

Creating C# Class Library (DLL) Using Visual Studio .NET

Step 6:- Add reference of DLL (CreatingDLL) which we created before few minutes.Creating C# Class Library (DLL) Using Visual Studio .NET

Creating C# Class Library (DLL) Using Visual Studio .NET

After adding reference of DLL, following windows will appear.Creating C# Class Library (DLL) Using Visual Studio .NET

Step 7:- Write code on button click of Windows Form Application. Before creating object and making method of Add DLL, add namespace CreatedDLL in project as bellow code.

using System;

using System.Windows.Forms;

using CreatingDLL;

namespace AccessingDLL

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void btnAdd_Click(object sender, EventArgs e)

        {

            Class1 c1 = new Class1();

            try

            {

                txtResult.Text = Convert.ToString(c1.sum(Convert.ToInt32(txtNumber1.Text), Convert.ToInt32(txtNumber2.Text)));

            }

            catch(Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

        }

    }

}

 

Step 8:- Now build the application and execute project and see output.Creating C# Class Library (DLL) Using Visual Studio .NET


Don't want to miss updates? Please click the below button!

Follow MindStick