Public Assemblies or Shared Assemblies in C#.Net

February 20, 2012 at 10:11 AM4MicrosoftSolutions


In my previous article we discussed about private assemblies in .Net. Today we discuss about shared or public assemblies.


Shared assembly(public assembly) also a collection of types and resources like private assembly. The main difference between shared assembly and private assembly is the single copy of a shared assembly used by several applications on a single machine where as in case of private assemblies, every time new copy is created for each application.


For example, if we take predefined assembly System.Windows.Forms.dll which is a shared assembly each time you are using this assembly in your application only reference is created for that assembly instead of copying that dll file into your application folder.


To create shared assembly you have to provide strong name for your assembly.  Strong name means combination of Assembly name, assembly version, culture(optional) and public key.



Strong Name =  [AssemblyTitle] + [AssemblyVersion] + [AssemblyCulture](optional) + [AssemblyKeyFileName]



Creating Public Assembly or Shared Assembly:


Here I will explain Shared Assembly by using calculator example which contain some mathematical operations such as addition, subtraction and multiplication.


Open Visual Studio => Create Project => Select Class Library and name it as SharedAssemblyExp

Rename the Class1.cs to Calculator.cs and below code.


namespace SharedAssemblyExp


    public class Calculator


        public int Add(int a, int b)


            return (a + b);



        public int Sub(int a, int b)


            return (a - b);



        public int Mul(int a, int b)


            return (a * b);





Now we have to create the public key file. For that open visual studio command tool enter below command.


Sn -k C:\myKey.snk


The public key file saves in C drive and output is as shown below.



As we need to provide strong name for our assembly, except public key all(assembly name, version, culture) are mentioned in AssemblyInfo.cs file. To add this public key file to our assembly, open AssemblyInfo.cs file which is located under Properties and add below code.


[assembly: AssemblyKeyFile("C:\\myKey.snk")]


Now build the project, dll will generate in bin/Debug folder if you build the project under debug mode or in bin/Release folder if you build the project in Release mode.


To make your assembly as shared assembly, place it in GAC(Global Assembly Cache) by using gacutil tool or just drag and drop your dll file in C:\Windows\assembly folder. Here we are using gacutil tool to place assembly in GAC. Open Visual studio command tool and execute below command to install assembly in GAC.


gacutil -i D:\SharedAssemblyExp\SharedAssemblyExp\bin\Debug\SharedAssemblyExp.dll


You will get output as shown below.


Now your assembly is available as public. Test your assembly with simple Console application.


Test Shared Assembly:


Open Visual studio=> Create Project => Select Console Application and name it as TestSharedAssembly.


Right Click on Solution Explorer and select Add Reference => select Browse option=> browse for your shared assembly SharedAssemblyExp.dll file(for me it is at D:\SharedAssemblyExp\SharedAssemblyExp\bin\Debug\).


Check properties of SharedAssemblyExp under Reference tab, you will find Copy Local property as False that means it is not maintaining the copy of assembly. Instead of copying it is just referencing assembly from GAC. Even you can check your application folder also you cannot find SharedAssemblyExp.dll file.


Add below code to check shared assembly functionality.


namespace TestSharedAssembly


    class Program


        static void Main(string[] args)


            SharedAssemblyExp.Calculator obj = new SharedAssemblyExp.Calculator();


            Console.WriteLine("Addition of {0} and {1} : {2}", 10, 20, obj.Add(10, 20));

            Console.WriteLine("Subtraction of {0} and {1} : {2}", 30, 20, obj.Sub(10, 20));

            Console.WriteLine("Multiplication of {0} and {1} : {2}", 10, 20, obj.Mul(10, 20));







The output displays as below.



You can remove or uninstalling the shared assembly from GAC(Glocal Assembly Cache) by using below code.


gacutil -u SharedAssemblyExp


The output display as shown below after executing the above command.



                                                                                                         (712.00 bytes)

                                                                                            (13.29 kb)

                                                                                            (22.47 kb)

Comments (1) -

Thanks a Lot, its a very nice article, I had a little confusion but now your article covered it up.

I want to know about .exe assemblies.


Add comment