blog

Home / DeveloperSection / Blogs / Introduction to .Net Assembly

Introduction to .Net Assembly

Manish Kumar2075 19-Jan-2017

It is a smallest unit of deployment of .net application or .net project.  It can be in the form of .dll(dynamic link library) and executable( .exe.) .It is typically a compiled output of our code. An assembly gives fundamental unit of physical code. It is a collection of all types and resources .Every time we make a  Windows Application, Windows Service, Class Library with Visual Basic .NET, that means we are creating an assembly.

There are two types of assemblies

  • Private Assembly          
  • Shared Assembly
  • Private Assembly

Private assembly is available for any single application. We cannot use private assembly for multiple application.

Now we take an example of creating private assembly

Step 1-

Select new project and select class library and name it ClassLibrary1

Introduction to .Net Assembly

Now add following code for creating library

using System;                  
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ClassLibrary1
{
    publicclassClass1
    {
        publicvoid str(int x)
        {
            Console.WriteLine("interger");
        }
 
        publicvoid str(string y)
        {
            Console.WriteLine("string");
        }
 
        publicstaticvoid Main()
        {
            Class1 obj = newClass1();
            obj.str("Manish");
         
            Console.ReadKey();
        }      }
}
 


Your library is created in bin folder with .dll extension. 

Now add another project of console application and now add reference and browse. When we select your library it will be added in your reference

Introduction to .Net Assembly

For adding assembly do following code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ClassLibrary1;
namespace ConsoleApplication4
{
    classProgram
    {
        publicstaticvoid Main()
        {
            Class1 obj = newClass1();
            obj.str("manish");
       
            Console.ReadKey();
        }
 
    }
}


In this first make the object of class of library and by object we can access


all method of library. And for adding library we use using Library Name

 

This is the example of private assembly we cannot access it in another

application.

Shared Assembly

Shared assembly is for many application we can use it in more than one

application. It is kept in global assembly cache. It is also known as strong

named assembly.

For creating strong named assembly

1-create a .dll file. (Using previous ClassLibrary1.dll)


2 Create a unique assembly name using SN utility (Shared Name utility).


  For creating this syntax for it is: sn -k filename.snk


 sn stands for strong name,

-k stands for key and

key is stored in filename.snk .

 

Difference between Private assembly and Shared Assembly

Private assembly cannot reference outside the scope of the folder where they are kept

Shared assembly is one that can be referenced by more than one application. 

Difference between Private assembly and Shared Assembly

Private assembly cannot reference outside the scope of the folder where

they are kept

Shared assembly is one that can be referenced by more than one

application.

You can also visit these useful related links

Where the assembly is stored in asp.net

How does assembly versioning in .net prevent DLL Hell


Updated 17-Mar-2018

Leave Comment

Comments

Liked By