Home > DeveloperSection > Beginner > DLL in C#

DLL in C#


C# C#  Dll 
Ratings:
0 Comment(s)
 688  View(s)
Rate this:

DLL in C#


A dynamic-link library (DLL) is a module that contains functions and data that can be used by another module (application or DLL). A DLL is a library that contains code and data that can be used by more than one program at the same time

A DLL can define two kinds of functions: exported and internal. The exported functions are intended to be called by other modules, as well as from within the DLL where they are defined. Internal functions are typically intended to be called only from within the DLL where they are defined. Although a DLL can export data, its data is generally used only by its functions. However, there is nothing to prevent another module from reading or writing that address.

 

DLLs provide a way to modularize applications so that their functionality can be updated and reused more easily. DLLs also help reduce memory overhead when several applications use the same functionality at the same time, because although each application receives its own copy of the DLL data, the applications share the DLL code.

 

Part1: Creating a Class Library (DLL)

Follow the steps to 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. 

 

 Class1 class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace stickSum
{
    public class Class1
    {
        private bool bTest = false;
 
        public Class1()
        {
            // // TODO: Add constructor logic here  
        }
 
        public void sumTest()
        {
 
        }
        public long sum(long val1, long val2)
        {
            return val1 + val2;
        }
 
        public bool Extra
        {
            get            {
                return bTest;
            }
            set
            {
                bTest = Extra;
            }
        } 
    }
}

 

 

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#.

Follow the steps to create a Console Application: select File->New->Project->Visual C# Projects->Console Application. I will test my DLL from this console application.


Add Reference of the Namespace: Now next step is to add reference to the library. You can use Add Reference menu option to add a reference. Go to Project->Add reference 

Import Namespace, Create Object of class and call its methods and properties.

 RectangleTest class

using System;
using System.Collections.Generic;
using System.Linq;                                                   
using System.Text;
using System.Threading.Tasks;
using stickSum;
 
namespace OoopsConcepts
{
    class RectangleTest
    {
        static void Main(string[]args)
        {
                                                                             
           Class1 stick = new Class1();
            long Res = stick.sum(24, 80);
            Console.WriteLine(Res.ToString());
            Console.ReadLine();
 
        }
    }
}

 

Output: 104


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

Follow MindStick