How to make an assembly or class library in C#

 
In this article we will learn that how to make as assembly in C#.net or how to make class library in C# using visual studio 2008.

Follow these steps to create an assembly:

  1. From File menu, click ‘New project’ (ctrl + N).
  2. From Templates window, select Class Library.
  3. Enter valid name of your Class Library (in this example we enter ClassLibrary1).
  4. Click Ok.
  5. Your class library is created successfully with class Class1.

    using System;
    using System.Collections.Generic;
    using System.Text;
     
    namespace ClassLibrary1
    {
        public class Class1
        {
        }
    }
  6. Rename Class1 to Rectangle.
  7. Write a new function ‘GetRectangleArea’ in ‘Rectangle’ class.
  8. using System;
    using System.Collections.Generic;
    using System.Text;
     
    namespace ClassLibrary1
    {
        public class Rectangle
        {
            public  int GetRectangleArea(int Width, int hieght)
            {
                int Area = Width * hieght;
                return Area;
            }
        }
    }

    GetRectangleArea function takes two parameters (both are integer, it makes a calculation and then returns the result as area of rectangle.

  9. Save project, click File menu and then Save All.
  10. Build the project using Build menu, click on Build Solution.

And now, next is to use this assembly or class library in windows application. Follow these steps:

  1. Add a new project (File–>Add–>New Project)
  2. From Templates window, select Windows Application.
  3. Enter valid name of your Windows Application (in this example we enter WindowsApplication1).
  4. Click Ok.
  5. Form1 will be appears. Add controls on this form like this:

  6. Add reference of ClassLibrary1 in windowsApplication1 (see below)
  7. Generate click event of button (you can generate click event through double click on button control)
  8. Write below code in the ButtonCalculate_Click

    private void ButtonCalculate_Click(object sender, EventArgs e)
            {
                ClassLibrary1.Rectangle classRectangle = new ClassLibrary1.Rectangle();
                textBoxArea.Text = (int.Parse(textBoxHeight.Text) * int.Parse(textBoxWidth.Text)).ToString();
            }

    you can run the WindowsApplication1 (make startup project as WindowsApplication1)