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 .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 .NET framework? Explain its key components.

Answer: .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, VC++.

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

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

CLR, which works like 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 set of APIs related to data access, cryptography, communication, mathematics, algorithms and application development, etc.



Question 2: What is an IL? Can we convert back IL code to 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 free utility by .NET, or using other third party tools like Reflector or IL spy, etc.

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

Answer:

Common Type System or CTS is the integral part of CLR by which .NET Framework provides support for multiple programming languages. CTS offers a type system that is common across all the languages. Due to this, two CTS-compliant languages can call each others code without any type of onversion. For example the size of integer and float variables are 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 alias as int and VB uses the integer, however in both the languages it will represent 4 byte integer value.

CLS or Common Language Specification is a set of rules, which must be followed by each language to be a .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 others code or class libraries.
 


Question 4: What is 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 which 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 and type safety, etc. Any miss in handling of the memory in unmanaged code (ActiveX components, Win32 APIs etc.) can lead to memory leak.
 


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 is called managed heap.

Each managed process gets its own heap and all the threads in the process uses 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 as 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 gets 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 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., .NET Framework provides Finalize and Dispose methods.
  • Dispose:

    Dispose is used for deterministic cleanup of the unmanaged resources. Developers need to call it explicitly by implementing IDisposable interface.

  • Finalize:

    Finalize is used to do 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 Dispose 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 and want to make sure they are disposed 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 of 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 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 previous step.

Question 8: What is the role of the JIT compiler in .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 runtime environment of .NET Framework. For example, it checks the values that are passed to parameters of any method.

Question 9: What is 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 application 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 the multiple processes on the same machine to share data with each other.

The MemoryMappedFile.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 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 file needs to be loaded in RAM; useful when working with large files.
  • Memory-mapped files enables to share data among multiple processes.

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

Up Next
    Ebook Download
    View all
    Learn
    View all