C# Assembly DLLs

  • An assembly is a block of .NET intermediate language (IL) code which was generated by a compiler in the .NET framework.
  • C# is one of the languages to create the source code of assemblies.
  • Mostly an assembly stands for a DLL (Dynamic Link Library) file or an EXE file which can be generated by C# compiler.
  • An assembly combines the data types, the implementations of the types and relevant resources together to form a logical unit in which realizes a bunch of functionalities.
  • An assembly can be run in the environment of the .NET Framework. For more details, you can check it here.

Example 01-48-01

The following example will show you how to create a assembly (DLL file) and how to call the methods of the assembly.

  1. In visual studio 2012, create a new project, choose "Class Library" and input the project name "MyLib" then click "OK" button.
  2. Create a C# class library project


  3. In the class Lib, create 2 methods plus and minus. Then click "Build" to generate MyLib.dll file.
  4. Create 2 methods in the class library


  5. Start a new instance of visual studio and create a new console project TestLib.
  6. In Solution Explorer, right click TestLib and choose Add Reference. The reference manager will be shown up as follows. Choose Browse and MyLib.dll then click "OK"
  7. Add a Reference in Visual Studio


  8. In Program.cs, add the code below. At last, run the code. The result will output "a+b=8".
  9. Call DLL Methods in a Class


The following is the MyLib class library.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;

namespace MyLib
{
    public class Lib
    {
        public int plus(int a, int b)
        {
            return a + b;
        }

        public int minus(int a, int b)
        {
            return a - b;
        }
    }
}
  • Line 5-16: Define a public class Lib.
  • Line 7-10: Define a method plus returning the result of a+b.
  • Line 12-15: Define another method minus returning the result of a-b.
  • There is no Main method in the class so you cannot run the program. After compiling, you will get MyLib.dll file.

The test program is shown below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using System;
using MyLib;

namespace TestLib
{
    class Program
    {
        static void Main(string[] args)
        {
            Lib lib = new Lib();
            Console.WriteLine("a+b={0}", lib.plus(3, 5));
            Console.WriteLine("a-b={0}", lib.minus(3, 5));
            Console.Read();
        }
    }
}

Output

a+b=8
a-b=-2
  • Line 2: use using keyword to include the MyLib class library to make the program use the defined class Lib directly.
  • Line 6-15: Define a class Program for testing.
  • Line 8: Start Main method.
  • Line 10: Lib class was defined in MyLib namespace. We'll create an instance of the class in this line.
  • Line 11: Call the plus method of the instance to output a+b=8.
  • Line 12: This line is added to check the result of the method minus. Here it outputs a-b=-2

In summary, we can create class libraries to realize specific functionalities. It can hide everything in a DLL file and can be invoked by other programs. Apparently, assemblies enhance the modularity and usability.