Preparing .NET Interview - Part 1 (Framework)

I am here to start a new series related to .NET interview preparation. I know there are a number of articles available on the Internet, but here I will use my own way to present the concepts in the easiest way possible and to help the readers to build a strong foundation.

Today, we will discuss the questions related to the .NET framework and explore their answers in an easy-yet-effective way.

So, let’s take the questions one by one.

Question 1. What is the .NET framework? Explain its key components.

Answer. The .NET framework is a comprehensive environment that allows developers to code, compile, run, and deploy applications. These applications can be of several types such as Console apps, Windows apps, Web apps, WCF, WPF, etc.

.NET Framework supports several programming languages including C#, VB.NET, and VC++.

.NET frameworks have two key components as in the following,

  • CLR (Common Language Runtime)
  • FCL (Framework Class Library) or BCL (Base Class Library)

CLR, which works as an application virtual machine, provides various services such as memory management, security, exception handling, type safety, thread management, etc.

On the other side, FCL provides a set of APIs related to data access, cryptography, communication, mathematics, algorithms application development, etc.

FCL

Question 2. What is IL? Can we convert back IL code back to the source code?

Answer. IL, or Intermediate Language, is also known as MSIL (Microsoft Intermediate Language). The .NET Framework comes with compilers of all. NET-supported languages. When we compile the source code, it gets converted into IL. The IL code is common for all programming languages.

IL is then converted to machine or native code by a Just-In-Time (JIT) compiler.

The logical flow can be presented as in the following.

C#/VB Code -----------------------> MSIL (Byte code) ------------------------>Machine/Native code.

Yes, we can convert IL code to source code again either using ILDASM, which is a free utility by .NET, or using other third-party tools like Reflector or IL spy, etc.

Question 3. What are the Common Type System (CTS) and Common Language Specification (CLS) and how are both related?

Answer. Common Type System or CTS is an integral part of CLR by which the .NET Framework provides support for multiple programming languages. CTS offers a type of system that is common across all the languages. Due to this, two CTS-compliant languages can call each other's code without any type of conversion. For example, the size of integer and float variables are the same across all . NET-compliant programming languages.

Having said that, each language can use aliases for the common or base data types provided by CTS. For example, for CTS type int32, C# uses an alias as int and VB uses the integer, however, in both the languages, it will represent a 4-byte integer value.

CLS or Common Language Specification is a set of rules, which must be followed by each language to be .NET- compliant. CLS is the part of CTS that enables interoperability between two . NET-compliant languages and hence languages supported by CLS can use each other's code or class libraries.

CTS

Question 4. What is the managed and unmanaged code?

Answer. Managed code is the code that is executed by the CLR instead of the operating system. The compiler first compiles the managed code to intermediate language code. This code is configuration-independent and hence can be executed on different machines.

Unmanaged code on the other hand is the code that is executed outside the CLR environment or by the operating system. It is directly compiled to native or machine code and hence depends on the machine configuration.

Managed code that is handled by CLR gets out-of-the-box services like automatic memory management, security, type checking, and exception handling, etc. These services help provide consistency in managed code applications developed in any. NET-supported languages.

Whereas in unmanaged code, the developer has to care about the allocation of memory, security type safety, etc. Any miss in the handling of the memory in unmanaged code (ActiveX components, Win32 APIs, etc.) can lead to a memory leak.

Operating system

Question 5. How does Garbage collection work? Explain its generations as well.

Answer. Once the garbage collector is initialized by CLR, it gets a part of memory to store and manage objects which are called the managed heap.

Each managed process gets its own heap and all the threads in the process use the same heap assigned to its process. The heap is divided into two types i.e. small object heap and large object heap (> 85 KB). These heaps are further organized into generations to handle short-lived and long-lived objects.

There are three generations of objects on the heap namely Generation 0, Generation 1, and Generation 2. Initially, newly created objects get allocated to Generation 0, and based on their longevity, they keep moving into further generations. The following are the details about each generation heap.

  • Generation 0: This is the earliest generation and contains short-lived objects. An example of a short-lived object is a temporary variable. Garbage collection occurs most frequently in this generation.
  • Generation 1: This generation mainly contains objects which are promoted from Generation 0 and their longevity is somewhere between short-lived and long-lived objects.
  • Generation 2: This generation holds long-lived objects. An example of a long-lived object is an object in a server application that contains static data that is live for the duration of the process.

Garbage collections occur either automatically when the situation demands or manually by commands as in the following:

  • System. GC.Collect(): Forces an immediate garbage collection of all generations.
  • System. GC.Collect(int generation): Forces an immediate garbage collection from generation 0 through a specified generation.
  • System. GC. Collect (int generation, GCCollectionMode mode)

Here generation can be 0, 1, or 2, and GCCollectionModecan be Default/ Forced/ Optimized. Default is Forced.

Question 6. State the differences between Dispose and Finalize

Answer. GC only releases managed resources. For releasing unmanaged resources like files, database connections, network connections, COM, etc., the .NET Framework provides Finalize and Dispose methods.

  • Dispose: Dispose of is used for the deterministic cleanup of unmanaged resources. Developers need to call it explicitly by implementing the IDisposable interface.
  • Finalize: Finalize is used to do the un-deterministic cleanup of the unmanaged resources. It is called by GC when we define destructor in the class.

Few points to remember

  • Developers should prefer to Dispose of it as it works in most cases and doesn’t have any performance impacts.
  • Finalize is just an extra safeguard in very rare situations when you are not sure whether all the objects are properly getting disposed of and want to make sure they are disposed of when the instance is no longer referenced by the application.
  • When using Dispose, call GC.SuppressFinalize method to avoid duplicate actions.

Question 7. What is CAS (Code Access Security)? What are the improvements in .NET 4?

Answer.CAS (Code access security) is a component of the .NET security model that prevents unauthorized access to resources and operations.

.NET Framework 4 has simplified the security system and gone through major changes.

Starting with the .NET Framework 4, the following changes are in effect.

  • Security policy is deprecated, however, permissions are still in use.
  • Application code has been divided into the following categories.
    • Security Critical code. (Safe or trusted code)
    • Security Transparent code (Unsafe code)
    • Security Safe Critical code (Bridge between Security Critical and Transparent code).
  • Permissions are determined by the grant set established by the application domain.
  • All partial-trust applications are classified as transparent.
  • Desktop and local intranet applications are granted full trust.

How to enable CAS?

  • In .NET Framework < 4.0. To turn on/off security, type the following command at the command prompt.
    caspol -security on
    caspol -security off
    
  • In the .NET Framework 4.0 and more, for using Caspol. exe, you first need to set the <LegacyCasPolicy> element to true along with the previous step.

Question 8. What is the role of the JIT compiler in the .NET Framework?

Answer. The JIT compiler is an important component of CLR that loads MSIL on target machines for execution. JIT compiler uses the CPU architecture of the target machine to convert the MSIL code into native code to execute a .NET application.

JIT compiler also enforces type-safety in the runtime environment of .NET Framework. For example, it checks the values that are passed to parameters of any method.

Question 9. What is a managed extensibility framework?

Answer. Managed extensibility framework (MEF) is a new library introduced in .NET 4.0. It provides better reuse of applications and components. MEF provides an easy way to host applications to consume external extensions without any configuration requirement.

Question 10. Explain memory-mapped files. What are the main benefits of such files?

Answer. Memory-mapped files (MMFs) are the special kind of files that allow you to map the content of a file to the logical address of an application. These files enable multiple processes on the same machine to share data with each other.

The MemoryMappedFile.The createFromFile method is used to obtain a MemoryMappedFile object that represents a persisted memory-mapped file from a file on disk.

This feature is available in the System.IO.MemoryMappedFiles namespace where a set of classes and enumerations are present to help in accessing and securing file mappings.

The benefits of Memory-mapped files are the following.

  • Better I/O performance as the changes are done in place.
  • Lazy loading as only the required portion of the file needs to be loaded in RAM; useful when working with large files.
  • Memory-mapped files enable to sharing of data among multiple processes.

I hope you have liked the article. I'm looking forward to your comments/suggestions.