.NET CLR Interview Questions and Answers

Question 1. What is the .NET Framework?

Answer: The .NET is a Framework, which is a collection of classes of reusable libraries given by Microsoft to be used in other .NET applications and to develop, build and deploy many types of applications on the Windows platform including the following: 

  • Console Applications
  • Windows Forms Applications
  • Windows Presentation Foundation (WPF) Applications
  • Web Applications
  • Web Services
  • Windows Services
  • Services-oriented applications using Windows Communications Foundation (WCF)
  • Workflow-enabled applications using Windows Workflow Foundation(WF)

That primarily runs on the Microsoft Windows Operating System.

Microsoft Windows operating system

Question 2. What is CLR?

Answer: The CLR stands for Common Language Runtime and it is an Execution Environment. It works as a layer between Operating Systems and the applications written in .NET languages that conform to the Common Language Specification (CLS). The main function of Common Language Runtime (CLR) is to convert the Managed Code into native code and then execute the program. The Managed Code compiled only when it is needed, that is it converts the appropriate instructions when each function is called. The Common Language Runtime (CLR)’s just in time (JIT) compilation converts Intermediate Language (MSIL) to native code on demand at application run time.

When a .NET application is executed at that time the control will go to Operating System, the Operating System create a process to load CLR.

The program used by the operating system for loading CLR is called runtime host, which are different depending upon the type of application that is desktop or web-based application i.e.

The runtime host for desktop applications is an API function called CorbinToRuntime.

The runtime host for web-based applications is the ASP.NET worker process (aspnet-wp.exe).

process to load CLR

CLR runtime engine comes with a set of services, which are classified as follows

CLR services

  • Assembly Resolver
  • Assembly Loader
  • Type Checker
  • COM marshaled
  • Debug Manager
  • Thread Support
  • IL to native compiler
  • Exception Manager
  • Garbage Collector

To know more about them follow the link:

Question 3. What is CTS?

Answer: The Common Type System (CTS) standardizes the data types of all programming languages using .NET under the umbrella of .NET to a common data type for easy and smooth communication among these .NET languages.

CTS

To implement or see how CTS is converting the data type to a common data type, for example, when we declare an int type data type in C# and VB.NET, then they are converted to int32. In other words, now both will have a common data type that provides flexible communication between these two languages.

For more details follow the link:

Question 4. What is CLS?

Answer: One of the important goals of the .NET Framework is to Support Multiple Languages. This is achieved by CLS. For multiple languages to interoperate, it is necessary that they should go on in common in certain features such as Types that are used. For example, every language has its own size and range for different data types. Thus CLS is the agreement among language designers and class library designers concerning these usage conventions.

CLS

Follow the link for more details: 

Question 5. What is a managed code?

Answer: The resource, which is within your application domain is, managed code. The resources that are within the domain are faster.

The code, which is developed in the .NET framework, is known as managed code. This code is directly executed by CLR with help of managed code execution. Any language that is written in .NET Framework is managed code.

Managed code uses CLR which in turn looks after your applications by managing memory, handling security, allowing cross-language debugging, and so on.

CLR

For more details follow the link:

Question 6. What is MSIL?

Answer: When we compile our .NET code then it is not directly converted to native/binary code; it is first converted into intermediate code known as MSIL code which is then interpreted by the CLR. MSIL is independent of hardware and the operating system. Cross-language relationships are possible since MSIL is the same for all .NET languages. MSIL is further converted into native code.

MSIL

For more details follow the link:

Question 7. What is JIT?

Answer: A Web Service or WebForms file must be compiled to run within the CLR. Compilation can be implicit or explicit. Although you could explicitly call the appropriate compiler to compile your Web Service or WebForms files, it is easier to allow the file to have complied implicitly. Implicit compilation occurs when you request the .asmx via HTTP-SOAP, HTTP-GET, or HTTP-POST. The parser (xsp.exe) determines whether a current version of the assembly resides in memory or in the disk. If it cannot use an existing version, the parser makes the appropriate call to the respective compiler (as you designated in the Class property of the .asmx page).

When the Web Service (or Web Forms page) is implicitly compiled, it is actually compiled twice. On the first pass, it is compiled into IL. On the second pass, the Web Service (now an assembly in IL) is compiled into machine language. This process is called Just-In-Time JIT compilation because it does not occur until the assembly is on the target machine.

JIT Types:

JIT Types

Follow the link for more details:

Question 8. What is a portable executable (PE)?

Answer: Every .NET program first compiles with an appropriate compiler like if we write a program in C# language then it gets compiled by C# compiler (i.e. csc.exe).

In the .NET framework, every program executes (communicate) in an operating system by using CLR (Common Language Runtime).

portable executable

Managed module is standard windows Portable Executable (PE) file which contains the following parts.

  • PE Header It is similar to the common object file format.
  • CLR Header This contains the CLR version required to run this managed module, location & metadata. This also contains the entry point of function i.e. the address of the entry point of the function.
  • Metadata This contains table information means variable with its data types and default values, functions/methods which are declared & defined in our program.

Follow the link for more details:

Question 9. What is the application domain?

Answer: An Application Domain is a logical container for a set of assemblies in which an executable is hosted. As you have seen, a single process may contain multiple Application Domains, each of which is hosting a .NET executable. The first appdomain created when the CLR is initialized is called the default AppDomain and this default one is destroyed when the Windows process is terminated. 

  • An AppDomain can be independently secured.
  • An AppDomain can be unloaded.
  • Independently configured.
  • No mutual intervention by multiple appdomains.
  • Performance

Windows process

How does an AppDomain get created? The AppDomain class is used to create and terminate Application Domains, load and unload assemblies and types, and enumerates assemblies and threads in a domain. The following table shows some useful methods of the AppDomain class:

Methods Description
CreateDomain() It allows us to create a new Application Domain.
CreateInstance() Creates an instance of the type in an external assembly.
ExecuteAssembly() It executes a *.exe assembly in the Application Domain.
Load() This method dynamically loads an assembly into the current app domain.
UnLoad() It allows us to unload a specified AppDomain within a given process.
GetCurrentThread() Returns the ID of the active thread in the current Application Domain.

In addition, the AppDomain class also defined as a set of properties that can be useful when you wish to monitor the activity of a given Application Domain.

Properties Description
CurrentDomain Gets the Application Domain for the currently executing thread.
FriendlyName Gets the friendly name of the current Application Domain.
SetupInformation Get the configuration details for a given Application Domain.
BaseDirectory Gets the directory path that the assembly resolver uses to probe for assemblies.

Follow the link for more details:

Question 10. What is an assembly?

Answer: An Assembly is a basic building block of .NET Framework applications. It is basically a compiled code that can be executed by the CLR. An assembly is a collection of types and resources that are built to work together and form a logical unit of functionality. An Assembly can be a DLL or Exe depending upon the project that we choose.

Assemblies are basically the following two types:

  1. Private Assembly
  2. Shared Assembly

Follow the link for more details:

Question 11. What are the contents of the assembly?

Answer: Assembly 

  • An Assembly is a basic unit of application deployment and versioning.
  • An Assembly is also called the building block of a .NET application.
  • An Assembly is either a .exe or .dll file.

An Assembly structure consists of the following parts:

  • Assembly manifest (name, language, and version).
  • CIL code (logic part).
  • Type information (Datatype).
  • Resources.

Follow the link for more details:

Question 12. What are the different types of assembly?

Answer: An Assembly contains metadata and manifest information. The reason for the emergence of the assembly concept was to overcome the common "DLL Hell" problem in COM. The assembly contains all the code, resources, metadata, and even version information. Metadata contains the details of every "type" inside the assembly. In addition to metadata, assemblies also have a special file called Manifest. It contains information about the current version of the assembly, culture information, public key token if available, and other related information.

There are in all 3 different types of assemblies:

  1. Private Assembly
  2. Shared or Strong named assembly
  3. Satellite assembly

Follow the link for more details:

Question 13. What is a dynamic assembly?

Answer: Technically, the act of loading external assemblies on demand is known as Dynamic Loading. Using the Assembly class, we can dynamically load both private and shared assemblies from the local location to a remote location as well as, explore its properties.

To illustrate dynamic loading, we are creating a console-based application that loads an external TestLib.dll assembly. During the execution, the application asks the user to specify the dynamic loading assembly name, and that reference is passed to the helper method that is responsible for loading the assembly.

Follow the link for more details:

Question 14. What is GAC?

Answer: The GAC is a shared location of the computer where we can put an assembly so that it will be accessible from many locations, I mean it is accessible from another project or application. It's always a good practice to provide a strong name to a public assembly, I mean the assembly to be registered in the GAC,  otherwise, the DLL hell problem may occur.

Problems that occurred

I have seen DLLs added to the GAC that you can't remove - very frustrating. I have seen registered DLLs into the cache - verified everything is there ok using ILDASM only to find the DLLs are no longer in the GAC.

Strongly naming the assembly

When doing this make sure you get the directory slashes \\ correct within the assembly file (assembly.cs). - if not, you will get errors whilst the code is looking for the .snk file. If you get errors that leave you scratching your head - the best bet is to remove the .snk file and start over.

Project References

Also be careful and watch where you build projects as the referenced DLLs can easily be built to the development instead of the release folder - sometimes even when you specify the release folder. This can be very, very frustrating.

Conclusion

My conclusion on using the GAC was only to use it if you really need to as it isn't the 'end of DLL hell' as first thought. Also only use it if you are using a DLL that is shared by other projects. Don't put it in the GAC if you don't have to.

For more details follow the link: 

Question 15. What is a garbage collector?

Answer: The Garbage Collector (GC) is the part of the .NET Framework that allocates and releases memory for your .NET applications. The Common Language Runtime (CLR) manages allocation and deallocation of a managed object in memory. C# programmers never do this directly, there is no delete keyword in the C# language. It relies on the garbage collector.

Example

Assume the managed heap contains a set of objects named A, B, C, D, E, F, and G. During garbage collection, these objects are examined for active roots. After the graph has been constructed, unreachable objects (that we will assume are objects C and F) are marked as garbage in reddish color in the following diagram.

manage heap

For more details follow the link:

Question 16. What are generations and how are they used by the garbage collector?

Answer: Basically the generation of Garbage Collection (GC) shows the life of objects, it means it defines how long an object will stay in the memory. It's categorized into the following three generations: 

  • Generation 0
  • Generation 1
  • Generation 2

generations

For more details follow the link:

More Interview Questions