Top 25 .NET Interview Questions And Answers

Microsoft .NET is a commonly used platform to develop and deploy different types of applications on the Windows operating system. This article is focused on to list down most frequently asked .NET Interview Questions and Answers. This article will be helpful for freshers as well as an experienced software developer. 

We cannot cover everything in a single article due to certain limitations, however, if you want to start your career into Microsoft Technologies or prepare for next technical interview then this article is the best starting point for you. So let’s begin to .NET interview questions and answers.

.net interview questions


Frequently asked .NET Interview Questions and Answers


Que 1. What is the .Net framework?

Answer: .Net framework is a platform developed by Microsoft for building and deploying various applications on Windows. It supports an object-oriented approach for building applications. It supports many languages such as C#, VB .Net, J#.NET, F#.NET, etc.  C# is the most popular language. The .NET framework contains inbuilt functionalities that include class, library, APIs which are used to build, deploy, and execute different types of applications.  

Que 2. What are the core components of .Net?

Answer: The following are the core components of .net.

  • NET Class Library
  • Common Language runtime
  • Application domains
  • .Net Framework
  • Cross-Language interoperability
  • Common Type System

Que 3. What is CLR?

Answer: CLR stands for Common Language Runtime. It is a runtime environment of the .Net Framework. It is one of the most important components of the .Net framework. Language-based compliers (example C#, VB.Net) converts code into Microsoft Intermediate Language (MSIL) and then CLR uses a just-in-time (JIT) compiler to converts the compiled code into Native Code.

CLR provides many features such as handles the exception, manages the memory, it loads and then executes the code, and converts the MSIL code into native code.

Que 4. What is CTS?

Answer: CTS stands for Common Type System. CTS has a set of rules which state how a data type should be declared, defined, and used in the program. It describes the data types that can be used by managed code. CTS facilitates cross-language integration in .Net.

Example: C# has an int data type and VB.NET has an Integer data type for the same purpose. Variable declared as an int in C# and Integer in VB.NET uses Int32 after compilation. 

.net interview questions

Que 5. What is CLS?

Answer: CLS stands for Common Language Specification.  CLS defines rules and restrictions that every language must follow which runs under the .NET framework to be a CLS Complaint language. CLS enables cross-language integration or interoperability between two CLS-Complaint languages such as C# and VB.NET. CLS is a subset of CTS (Common Type System) that means all the rules in the CTS apply to CLS.

Que 6. What is JIT?

Answer: JIT stands for Just In Time. JIT is a part of CLR in .Net. JIT is a compiler that is responsible to convert intermediate code into native code during execution. Native code means hardware specifications that can be read by the CPU. 

Que 7. What are the types of Just-In-Time Compiler?

Answer: Following are the three types of JIT compiler:

  • Pre-JIT Compiler: Compiler compiles all the source code into the machine/native code in a single compilation cycle. This compilation process is performed during application deployment.

  • Normal JIT Compiler: Compiler compiles only those source code methods that are required at run time. After that, they are stored in the cache and used from cache whenever they are called again.

  • Econo JIT Compiler: Compiler compiles only those source code methods that are required at run time then it removes complied code from memory after execution.

Que 8. What is MSIL?

Answer: MSIL stands for Microsoft Intermediate Language or Intermediate Language (IL). During the compile time, the compiler converts the source code into Microsoft Intermediate Language (MSIL). MSIL is a CPU-independent set of instructions that can be efficiently converted to the native code. JIT compiler converts the Microsoft Intermediate Language (MSIL) code into native code to the Operating System during runtime.

Que 9. What is Managed and Unmanaged code?

Answer: 
Managed code is a code that is executed by CLR (Common Language Runtime). It is considered as managed because of the .Net framework which internally uses the garbage collector to clean up the memory.

Unmanaged code is any code that is executed by application runtime of any ANOTHER framework apart from .Net. The application runtime will take care of memory, security, and other performance operations.

Example: Managed code is managed by and runs inside the CLR and needs the .NET Framework to execute. Unmanaged code,  does not need the CLR to execute. Unmanaged code is formulated from a language-independent of the .NET Framework and therefore uses its independent environment for execution and compiling.

Que 10. What is Assembly? What are the different types of Assemblies?

Answer: Assembly is the smallest unit of deployment of a .NET application. Assemblies form the fundamental units of deployment, version control, reuse, activation scoping, and security permissions for .NET-based applications. In other words, assembly is a collection of Exe and DLLs. It is portable and executable.

Types of Assemblies:

  • Private Assembly: As the name itself suggests, it is accessible only to the application. It is generally stored in the application root folder.
  • Shared Assembly: Shared Assembly can be shared by multiple applications. It is installed in the GAC (Global assembly cache).
  • Satellite Assemblies: Satellite Assemblies provide support for multiple languages which is based on different cultures. These are kept in different modules based on the different categories available.

Que 11. What are the parts of an Assembly?

Answer: Following are the parts of an Assembly:

  • Manifest: It contains the assembly’s name, version, culture, strong name (if applicable), a list of the files it comprises, and details of other assemblies that it references. In other words, it contains assembly metadata.
  • Type Metadata: Defines all types, their properties, and methods.
  • MSIL Code: Microsoft Intermediate Language code.
  • Resources: List of other resources files (like icons, images, etc.).

Que 12. What is Reflection?

Answer: Reflection is the ability of a code to access the metadata of the assembly during runtime. Metadata refers to information about objects, methods. Assemblies contain modules, modules contain types, and types contain members. Reflection provides objects that encapsulate assemblies, modules, and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object. You can then invoke the type’s methods or access its fields and properties.

The namespace System. Reflection contains methods and classes that manage the information of all the loaded types and methods. Reflection is implemented in two steps, first, we get the type of the object, and then we use the type to identify members such as methods and properties.

Example:

Type type = Student.GetType();
System.Reflection.MemberInfo memberInfo = type.GetMethod("StudentDetails"); 

Type class is used to get the type of class. Once we get a type of class, the other information of the class can be easily accessed. MemberInfo class obtains information about the attributes of a member and provides access to member metadata. memberInfo object tries to find a method with the name StudentDetails in the Student class. Information visible in the class view can be accessed using reflection in .Net.

Class view of Student class:

.net interview questions

Learn Reflection in detail here.
This one of the frequently asked .NET interview questions

Que 13. What is GAC?

Answer: GAC stands for Global Assembly Cache. Every computer where the CLR is installed has a reserved area for machine-wide code which is known as GAC. 

GAC is a folder in the Windows directory to store the .NET assemblies that are specifically designated to be shared by multiple applications on a system. GAC tool called Gacutil.exe is used to register any assembly into GAC so that assembly can be shared among multiple applications. The GAC is automatically installed with the .NET runtime.

Que 14. What is a Garbage Collection in .Net?

Answer: Garbage Collection is a feature in .Net to free the unused managed code objects in the memory and free the space to the process. Each time we create a new object by using the new keyword, CLR allocates memory for the object from the managed heap. If address space is available in the managed heap, the runtime continues to allocate space for new objects, but some point memory space needs to be free for new objects. The garbage collector is responsible to perform a collection to free some memory. This is one of the important .net interview questions.

Que 15. What is Serialization in .Net?

Answer: Serialization is the process of converting an object into a stream of bytes to store the object into memory, database, or a file. The main purpose of Serialization is to save the state of an object so it can be recreated it when needed. The reverse process is called Deserialization.

Any class which is marked with the attribute [Serializable] will be converted to its binary form. Namespace System.Runtime.Serialization can contain classes for serialization.

Types of Serialization:

  • XML Serialization: It serializes all the public properties to the XML document. Since the data is in XML format, it can be easily read and manipulated in various formats. The classes reside in System.sml.Serialization.
  • SOAP: Classes reside in System.Runtime.Serialization. Similar to XML but produces a complete SOAP compliant envelope that can be used by any system that understands SOAP.
  • Binary Serialization: Allows any code to be converted to its binary form. It can serialize and restore public and non-public properties. It is faster and occupies less space.

Uses for Serialization: Serialization allows the developer to save the state of an object and re-create it as needed, providing storage of objects as well as data exchange. Developers can perform the following action by using serialization:

  • Sending the object to a remote application by using a web service.
  • Passing an object from one domain to another.
  • Passing an object through a firewall as a JSON or XML string.
  • Maintaining security or user-specific information across applications

Que 16. What is the difference between namespace and assembly?

Answer: An assembly is a physical grouping of logical units whereas namespace is a logical group of related classes. An assembly is an output unit or unit of deployment. Assembly may contain more than one namespace. 

Que 17. What is the difference between Managed and Unmanaged Code?

Answer: Following are the differences between Managed and Unmanaged Code:

ComparisonManaged CodeUnmanaged Code
DefinitionCode which is managed by CLR (Common Language Runtime) is called Managed CodeCode which is not managed by CLR (Common Language Runtime) is called unmanaged Code
Memory ManagementIt provides Garbage Collection service for memory management hence no memory leakPossibility of memory leak as Garbage Collection service does not support
CompilationThe source code is compiled into MSILThe source code is compiled into the native language
LanguagesCode writer in .Net supported languages such as C#, VB.Net is managed codeCode written in C or C++ is unmanaged code

Que 18. What is the difference between assembly and namespace?

Answer: An assembly is a physical grouping of logical units whereas namespace is a logical group of related classes. An assembly is an output unit or unit of deployment. Assembly may contain more than one namespace.

Que 19. What are Globalization and Localization?

Answer: Application has to reach many people in the globe hence it’s always recommended that design an application that supports multiple languages. This can be achieved through Localization and Globalization in .Net.

Globalization: Globalization is the process of designing an application to support different languages so that it can be used by users from across the globe (multiple cultures). Globalization can be implemented in existing applications as well as to multiple cultures.

Localization: Localization means customizing a globalized application for specific cultures or regions. Microsoft.Extensions.Localization is used for localizing the app content.  

Que 20: What is caching in .Net?

Answer: Caching is a very important technique to boost the performance of ASP.NET application. Caching is a technique of storing frequently used data into memory, so that, when the same data is required next time, it could be directly retrieved from the memory instead of being generated by the application.

Types of caching:

  • Page (Output) caching: It caches the entire page generated by the request
  • Data caching: It caches data from a data source.
  • Fragment (Partial-Page Output) Caching: It caches the portion of the page generated by the request.

Que 21. What is the DISPOSE method in .Net?

Answer: .Net uses Garbage Collection internally to free managed resources, however, to free unmanaged resources, the developer has to take responsibility. DISPOSE method plays an important role to free unmanaged resources. 

DISPOSE method is used to free unmanaged resources (files, database connections, etc.) used by code. It belongs to the IDisposable interface. DISPOSE () is called by the user code explicitly and the class implementing dispose method must implement an IDisposable interface. Once DISPOSE () called you can inform GC to skip finalize on this instance by calling GC.SuppressFinalize(this) method.

Suppress later calls to the finalizer from within the Dispose method using the GC.SuppressFinalize method

Example:

using System;
namespace TestConsoleApp
{
    class Student: IDisposable
    {
        private bool _disposed = false;
        public void Dispose()
        {
            // Dispose unmanaged resources.
            Dispose(true);
            // Suppress finalization.
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed && disposing)
            {
                //dispose managed resources
            }
            //dispose unmanaged resources
            _disposed = true;
        }
    }
} 

Que 22. What is the difference between Dispose and Finalize Method?

Answer: Following are the differences between Dispose and Finalize Methods: 

ComparisonDispose()Finalize()
UseIt is used to free unmanaged resources like files, database connections, etc. at any time.It can be used to free unmanaged resources (when you implement it) like files, database connections, etc. held by an object before that object is destroyed.
Interface/ClassIt belongs to the IDisposable interface.It belongs to Object class.
ImplementationIt's implemented by implementing the IDisposable interface Dispose() method.It's implemented with the help of a destructor in C++ & C#.
ImplementationExplicitly, it is called by user code and the class which is implementing the dispose method must have to implement an IDisposable interface.Internally, it is called by the Garbage Collector and cannot be called by user code.
PerformanceNo performance costs associated with the Dispose method.Performance costs associated with the Finalize method since it doesn't clean the memory immediately and is called by GC automatically.

Que 23. What is the difference between constants and readonly?

Answer: 
Constant
variables are declared and initialized at compile time. They must have a value at compilation time. The value can’t be changed afterward.

Readonly variables can be initialized in the declaration or by code in the constructor. Readonly variables value cannot change in member Function.

Que 24. What is the difference between .NET Core and .NET Framework?

Answer: Following are the differences between .NET Core and .NET Framework:

Comparison.NET Core.NET Framework
Open Source.NET Core Framework is not Open Source and Microsoft accept third party contributors.NET Framework is not fully Open Source and Microsoft doesn’t accept third party contributors
Cross-platform.NET Core is cross-platform.NET Framework is not cross-platform
Performance.NET Core is faster.NET Framework is slow compared to .NET Core
Microservices You can develop and deploy scalable microservices using mixed technology in .NET CoreYou cannot develop and deploy microservices in different languages using .NET Framework
Deployment.NET Cord support for the in-app deployment model.NET Framework does not support the in-app deployment model

Que 25. What is the difference between .NET Core and .NET Framework?

Answer: String and StringBuilder both use to store string value, but both have many differences on the bases of instance creation and performance:

String: The string is an immutable object. Immutable like when we create string object in code so we cannot modify or change that object in any operations like insert new value, replace or append any value with the existing value in the string object when we have to do some operations to change string simply it will dispose the old value of string object and it will create a new instance in memory for hold the new value in string object like:
Example:

static void Main(string[] args)
{
    string str = "Hello";
    str += " Users,"; //this create new instance of str object
    str += " Good Morning!"; //create new instance of str object
    Console.WriteLine(str);
} 
  • It’s an immutable object that holds string value.
  • Performance-wise string is slow because it creates a new instance to override or change the previous value.
  • String belongs to System namespace.

StringBuilder: System.Text.StringBuilder is a mutable object which also holds the string value, mutable means once we create a System.Text.StringBuilder object we can use this object for any operation like insert value in an existing string with insert functions also replace or append without creating a new instance of System.Text.StringBuilder for every time so it’s using the previous object hence it work fast compare to System.String. 
Example:

static void Main(string[] args)
{
    StringBuilder sb = new StringBuilder();
    sb.Append("Hello");
    sb.Append(" Users,");
    sb.Append(" Good Morning!");
    Console.WriteLine(sb);
} 
  • StringBuilder is a mutable object.
  • Performance-wise StringBuilder is very fast because it will use the same instance of StringBuilder object to perform any operation like insert value in the existing string.
  • StringBuilder belongs to System.Text.StringBuilder namespace.



Summary:

These are very basic .net interview questions and answers that every developer or beginner should learn. I am sure this topic will be very helpful to everyone.