A One-Stop Solution to C# Assembly With a Guide and Examples

One type of programme file is the DLL, or Dynamic Link Library, which stores code that can be used by other programmes. In this way, several applications can use the features contained in a single file simultaneously. Files with the DLL extension are not executable in and of themselves, unlike their EXE counterparts. Instead, they must be referenced from within other programmes. Some DLLs may even have the.EXE file extension because of their similarity to EXEs. Some Dynamic Link Libraries use the.OCX,.CPL, or.DRV file extension in addition to the more common.DLL. 

In this 'C# Assembly' tutorial, you will learn the major technical aspects of Assemblies and the fundamentals involving ‘.dll’ files and ‘.exe’ files.

Learn the Ins & Outs of Software Development

Caltech Coding BootcampExplore Program
Learn the Ins & Outs of Software Development

What Is C# Assembly?

An assembly is a unified set of capabilities formed by combining types and resources. Assembly can refer to either a dynamic link library (DLL) or an executable file (.exe), depending on its intended use. Implementation, versioning, repurposing, installation scoping, and privacy authorizations are all handled primarily through assemblies in “.NET-based applications”. 

Information about an assembly can be retrieved programmatically using reflection. Assembly files contain the data that the common language runtime needs to understand how various types are implemented. According to the runtime, a type only exists within the scope of an assembly.

Content of C# Assembly

There is seven content of the C# Assembly in the .NET Framework.

  • Manifest:

All information about subassemblies that make up the assembly itself is recorded in the manifest. Metadata describes the information included in the manifest.

  • Assembly Name:

It's useful for the scope of visibility and version control.

  • Version Information: 

The assembly's version number is part of its unique identifier.

  • Types: 

Methods, classes, properties, events, and attributes have distinct remits and boundaries specified by their types.

  • Locale: 

Information on language and culture is defined.

  • Cryptographic Hash: 

The hash encoded with the public key is being used as a check for compatibility and safety.

  • Security Permissions: 

The permissions within the assembly determine the privileges that can be granted for all aspects of the assembly's content.

Now let's discuss the Assembly Cache.

Learn the Ins & Outs of Software Development

Caltech Coding BootcampExplore Program
Learn the Ins & Outs of Software Development

What Is an Assembly Cache? 

When you set up the .NET framework, you make a repository available. This storage space has been dubbed the Assembly Cache.

The cache has both local and network components. The global cache is where all of the shared assemblies live. Data that should be kept secret for the sake of security might be found in the private folder. Your application's private cache is also where it stores any files it downloads while running.

There must be unique namespaces for all assemblies in the shared global cache. The assembly's unique identity is encoded in the folder names. Locate the Assembly Cache in the assembly folder of your Windows directory.

Types of C# Assembly

Assembly-article-types-img1.

There are two types of assemblies in the .Net framework

  1. Process assembly: Process assembly has .exe files
  2. Library assembly: Library assembly has .dll files.

Assembly-article-types-img2

Library assembly is again defined in two Types -

  1. Private Assembly
  2. Shared or Public Assembly

Now let's discuss them in detail. Let's start with the Private Assembly.

Learn the Ins & Outs of Software Development

Caltech Coding BootcampExplore Program
Learn the Ins & Outs of Software Development

Private Assembly 

To use a private assembly's features and power, a copy of the assembly must be placed in each application folder in which it is used. When we only want to share a private assembly with other applications, we only need to copy the files into the BIN folder.

So far, you have learned about the Private Assembly. Now Let’s discuss the shared Assembly.

Shared Assembly

Shared Assembly can also be referred to as "Public Assemblies." It is unnecessary to make a second copy of the assembly for use within the application itself; instead, keep the original within the system folder. In other words, you can skip copying into each individual application folder. 

So far, you have learned about the Private and Shared Assembly. Now Let’s discuss Global Assembly Cache.

Global Assembly Cache

When an assembly is needed for more than one project or application, we must create it with a strong moniker and keep it in GAC or the Assembly folder by installing it with the GACUtil command.

Learn the Ins & Outs of Software Development

Caltech Coding BootcampExplore Program
Learn the Ins & Outs of Software Development

Implementation of C# Assembly

Let's create a private assembly.

Assembly-article-demo-img1

We will create a new project class library. 

Assembly-article-demo-img2.

Let's name it PrivateAssembly 

Assembly-article-demo-img3.

Let's choose .net core 3,1 and hit create. 

Now let's write some code.

Code:

using System;  

using System.Collections.Generic;  

using System.Linq;  

using System.Text;  

namespace PrivateAssembly  

{  

    public class PrivateAssembly  

    {  

               public int Add(int n1, int n2)  

      {  

                int res = n1 + n2;  

                return res;  

            }      

        public int diff(int n1, int n2)  

      {  

            int res = n1 - n2;  

                return res;   

        }  

  

    }  

}  

Assembly-article-demo-img4

Now let's add a new asp.net web application project in this. 

Assembly-article-demo-img5

We will choose asp.net web application

Assembly-article-demo-img6.

Let's Name it PrivateAssemblyWebform.

Assembly-article-demo-img7

We will select web form as we are creating a webform and hit create

Then we will just build both projects.

Assembly-article-demo-img8.

Now let's build the private assembly project. We will select and add a reference to private assembly for this web application. 

Assembly-article-demo-img9

We will go into the project category and select the “PrivateAssembly”.

Assembly-article-demo-img10

Now let's check the bin folder in the web forms directory. And here, we have our private assembly.

By now, you must have a good knowledge and command of the technical aspects of C# Assembly. 

Learn the Ins & Outs of Software Development

Caltech Coding BootcampExplore Program
Learn the Ins & Outs of Software Development

Next Steps

The next lesson in your C# training can be "C# Reflection". A computer program can improve its performance by reflecting on its actions and source code. Reflection in C#, for instance, lets you inspect and change runtime data about your program, which has enormous potential but which you may not yet appreciate. C# reflection's features, also known simply as "reflection," are incredibly robust and deceptively easy to use.

Simplilearn is the world's most popular online Bootcamp for learning digital economy skills, and it's here to help you do that. Digital marketing and data science are just a few subjects you will cover in-depth in the online courses.

You've come to the right place if you're interested in learning more about software development and working in the field. The Caltech CTME and the Indian Institute of Technology, Kanpur, have collaborated with Simplilearn to deliver their Software Development courses. In addition to more advanced topics like Competitive Programming, these courses teach the fundamentals of data structures and algorithms. As a software developer, data structures, including trees, graphs, and queues, will be taught to you.

The comments section below is open to questions and comments about this 'C# Assembly' tutorial. Happy learning!

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, OPM3 and the PMI ATP seal are the registered marks of the Project Management Institute, Inc.