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


Creating a DLL using Visual C# is piece of cake. Believe me it's much easier than VC++.
I have divided this tutorial in two parts.
  1. Building a Class Library,
  2. Building a client application to test the DLL.
Part 1: Creating a Class Library (DLL)
Create an Empty Class Library Project
Select File->New->Project->Visual C# Projects->Class Library. Select your project name and appropriate directory using Browse button and click OK. See Figure 1.
Create Class Library
Figure 1.
Project and Its files
The Solution Explorer adds two C# classes to your project. First is AssemblyInfo.cs and second is Class1.cs.  We don't care about AssemblyInfo. We will be concentrating on Class1.cs. See Figure 2.
Solution Explorer
Figure 2.
The mcMath Namespace
When you double click on Class1.cs, you see a namespace mcMath. We will be referencing this namespace in our clients to access this class library.
  1. using System;
  2. namespace mcMath
  3. {
  4.     /// <summary>
  5.     /// Summary description for Class1. 
  6.     /// </summary>
  7.     public class Class1 
  8.     {
  9.         public Class1()
  10.         {
  11.             //
  12.            // TODO: Add constructor logic here
  13.            //
  14.         }
  15.     }
  16. }
Now build this project to make sure every thing is going OK. After building this project, you will see mcMath.dll in your project's bin/debug directory. 
Adding Methods 
Open ClassView from your View menu. Right now it displays only Class1 with no methods and properties. Let's add one method and one property. See Figure 3.
Add Methods in C#
Figure 3.
Right-click on Class1->Add->Add Method...  See Figure 4.
Add Methods in C#
Figure 4.
C# Method Wizard pops up. Add your method name, access type, return type, parameters, and even comments.  Use Add and Remove buttons to add and remove parameters from the parameter list respectively. I add one test method called mcTestMethod with no parameters. See Figure 5.
C# Method Wizard
Figure 5.
I am adding one more method long Add( long val1, long val2 ). This method adds two numbers and returns the sum. Click Finish button when you're done. See Figure 6.
C# Method Wizard
Figure 6.
The above action adds two method to the class and methods look like following listing:
  1. /// <summary>
  2. /// //This is a test method
  3. /// </summary>
  4. public void mcTestMethod()
  5. {
  6. }
  7. public long Add(long val1, long val2)
  8. {
  9. }
Adding Properties
Open C# Property Wizard in the same manner as you did in the case of method and add a property to your class. See Figure 7.
C# Property Wizard
Figure 7.
This action launches C# Property Wizard. Here you can type your property name, type, and access. You also have options to choose from getting only, set only, or get and set both. You can even select if a property is static or virtual. I add a property Extra with public access and bool type and get/set option set. See Figure 8.
C# Property Wizard
Figure 8.
After adding a method and a property, our class looks like Figure 9 in Class View after expanding the class node.
add method and property in c#
Figure 9.
If you look your Class1 class carefully, Wizards have added two functions to your class. 
  1. /// <summary>
  2. /// //This is a test property
  3. /// </summary>
  4. public bool Extra
  5. {
  6.     get
  7.     { 
  8.         return true;
  9.     }
  10.     set
  11.     { 
  12.     }
  13. }
Adding Code to the Class 
Add this code (bold) to the methods and property now. And now I want to change my Class1 to mcMathComp because Class1 is quite confusing and it will create a problem when you will use this class in a client application. Make sure you change the class name and its constructor both.
Note: I'm not adding any code to mcTestMethod, You can add any thing if you want.
  1. using System;   
  2. namespace mcMath   
  3. {   
  4.     /// <summary>   
  5.     /// Summary description for Class1.   
  6.     /// </summary>   
  7.     public class mcMathComp   
  8.     {  
  9.         private bool bTest = false;  
  10.         public mcMathComp()   
  11.         {   
  12.             // TODO: Add constructor logic here   
  13.         }   
  14.         /// <summary>   
  15.         /// //This is a test method   
  16.         /// </summary>   
  17.         public void mcTestMethod()   
  18.         { }  
  19.         public long Add(long val1, long val2)   
  20.         {   
  21.             return val1 + val2;   
  22.         }  
  23.         /// <summary>   
  24.         /// //This is a test property   
  25.         /// </summary>   
  26.         public bool Extra   
  27.         {   
  28.             get   
  29.             {   
  30.                 return bTest;   
  31.             }   
  32.             set   
  33.             {   
  34.                 bTest = Extra ;   
  35.             }   
  36.         }   
  37.     }   
  38. } 
Build the DLL
Now build the DLL and see bin\debug directory of your project. You will see your DLL. Piece of cake? Huh? :). 
Part 2: Building a Client Application
Calling methods and properties of a DLL from a C# client is also an easy task. Just follow these few simple steps and see how easy is to create and use a DLL in C#.
Create a Console Application 
Select File->New->Project->Visual C# Projects->Console Application. I will test my DLL from this console application. See Figure 10.
use DLL in c#
Figure 10.
Add Reference of the Namespace
Now next step is to add a reference to the library. You can use the Add Reference menu option to add a reference. Go to Project->Add reference. See Figure 11.
add reference in c#
Figure 11.
Now on this page, click the Browse button to browse your library. See Figure 12.
add reference in c#
Figure 12.
Browse for your DLL, which we created in part 1 of this tutorial, and click Ok. See Figure 13.
add reference in c#
Figure 13.
Add Reference Wizard will add reference to your library to the current project. See Figure 14.
Add Reference Wizard
Figure 14.
After adding reference to mcMath library, you can see it as an available namespace reference. See Figure 15.
Solution Explorer
Figure 15.
Call mcMath Namespace, Create Object of mcMathComp, and call its methods and properties.
You are only one step away to call the methods and properties of your component. You follow these steps:
1. Use namespace
Add using mcMath in the beginning for your project.
  1. using mcMath; 
2. Create an Object of mcMathComp
  1. mcMathComp cls = new mcMathComp(); 
3. Call Methods and Properties
Now you can call the mcMathComp class method and properties as you can see I call Add method and return result in lRes and print out result on the console.
  1. mcMathComp cls = new mcMathComp();
  2. long lRes = cls.Add( 23, 40 );
  3. cls.Extra = false;
  4. Console.WriteLine(lRes.ToString()); 
Now you can print out the result.
The entire project is listed in the following Listing:
  1. using System;
  2. using mcMath;
  3. namespace mcClient
  4. {
  5.     /// <summary>
  6.     /// Summary description for Class1.
  7.     /// </summary>
  8.     class Class1
  9.     {
  10.         /// <summary>
  11.         /// The main entry point for the application.
  12.         /// </summary>
  13.         [STAThread]
  14.         static void Main(string[] args)
  15.         {
  16.             mcMathComp cls = new mcMathComp();
  17.             long lRes = cls.Add( 23, 40 );
  18.             cls.Extra = false;
  19.             Console.WriteLine(lRes.ToString());
  20.         }
  21.     }
  22. }
Now build and run the project. The output looks like Figure 16.
C# Class Library
Figure 16.

Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.