articles

Home / DeveloperSection / Articles / DLL in C#

DLL in C#

Anonymous User4964 05-May-2016

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
{
    publicclassClass1
    {
        privatebool bTest = false;
 
        public Class1()
        {
            // // TODO: Add constructor logic here  
        }
 
        publicvoid sumTest()
        {
 
        }
        publiclong sum(long val1, long val2)
        {
            return val1 + val2;
        }
 
        publicbool 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
{
    classRectangleTest
    {
        staticvoid Main(string[]args)
        {
                                                                             
           Class1 stick = newClass1();
            long Res = stick.sum(24, 80);
            Console.WriteLine(Res.ToString());
            Console.ReadLine();
 
        }
    }
}

 

Output: 104


Updated 27-Mar-2018
I am a content writter !

Leave Comment

Comments

Liked By