Google News
logo
.Net Interview Questions
.NET is a developer platform to build a variety of applications for web, mobile, desktop, and IoT. It supports various languages like C#, F#, Visual Basic, J#, C++, etc. for building the applications.
* .NET framework-based applications that are written in supportive languages like C#, F#, or Visual basic are compiled to Common Intermediate Language (CIL).

* The .NET framework is a software development platform that supports an object-oriented approach. It offers services, like memory management, networking, security, and type safety.

* Compiled code is stored in the form of an assembly file that has a .dll or .exe file extension.

* When the .NET application runs, Common Language Runtime (CLR) takes the assembly file and converts the CIL into machine code with the help of the Just In Time(JIT) compiler.
The major components .NET framework are given below:
 
Common Language Runtime(CLR) :
* It is an execution engine that runs the code and provides services that make the development process easier.
* Services provided by CLR are memory management, garbage collection, type safety, exception handling, security, and thread management. It also makes it easier for designing the applications and components whose objects interact across the languages.
* The programs written for the .NET Framework are executed by the CLR regardless of programming language. Every .NET Framework version is having CLR.

Framework Class Library(FCL):
* It has pre-defined methods and properties to implement common and complex functions that can be used by .NET applications. It will also provide types for dates, strings, numbers, etc.
* This class library includes APIs for database connection, file reading and writing, drawing, etc.

Base Class Library(BCL):
* The Base Class Library(BCL) has a huge collection of libraries features and functions that are helpful in implementing various programming languages such as C#, F#, Visual C++, etc., in the .NET Framework.
* BCL is divided into two parts. They are:

* User-defined class library : It includes Assemblies.
* Assembly : A .NET assembly is considered as the major building block of the .NET Framework. An assembly in the CLI(Common Language Infrastructure) is a logical unit of code, which is used for security, deployment, and versioning. Assembly can be defined in two forms namely Dynamic Link Library(.dll) and executable(.exe) files.
When compilation of the .NET program takes place, metadata with Microsoft Intermediate Language(MSIL) will be generated and will be stored in a file called Assembly.

* Predefined class library : It contains namespace.
* Namespace : It is the collection of pre-defined methods and classes that are present in the .Net Framework. A namespace can be added to a .NET program with the help of “using system”, where using represents a keyword and system represents a namespace.

Common Type System(CTS) :
* CTS specifies a standard that will mention which type of data and value can be defined and managed in memory during runtime.
* It will make sure that programming data defined in different languages should interact with each other for sharing the information. For example, in VB.NET we define datatype as integer, while in C# we define int as a data type.
* It can be used to prevent data loss when you are trying to transfer data from a type in one language to its equivalent type in another language.

Common Language Specification (CLS) :
* Common Language Specification (CLS) is a subset of CTS and defines a set of rules and regulations to be followed by every .NET Framework’s language.
* A CLS will support inter-operability or cross-language integration, which means it provides a common platform for interacting and sharing information. For example, every programming language(C#, F#, VB .Net, etc.) under the .NET framework has its own syntax. So when statements belonging to different languages get executed, a common platform will be provided by the CLS to interact and share the information.
There are a lot of components that make up the .NET framework, and some of them are as follows :
 
* .NET Class Library
* .NET Framework
* Language Runtime
* Application Domain
* Profiling
JIT stands for Just in Time. It is a compiler that is used to convert intermediate code into native code easily.
 
In .NET, during execution, the code is converted into the native language, also called the byte code. This is processed by the CPU, and the framework helps with the conversion.
EXE and DLLs are assembly executable modules.
 
EXE is an executable file that runs the application for which it is designed. An EXE is produced when we build an application. Therefore the assemblies are loaded directly when we run an EXE. However, an EXE cannot be shared with the other applications.
 
Dynamic Link Library (DLL) is a library that consists of code that needs to be hidden. The code is encapsulated inside this library. An application can consist of many DLLs which can be shared with the other programs and applications.
There are 3 types of JIT Compilers :
 
i) Pre-JIT compiler : It compiles all the source code into the machine code in a single compilation cycle, i.e. at the application deployment time.
 
ii) Normal JIT Compiler : The source code methods required at run-time are compiled into machine code and stored in the cache to be called later.
 
iii) Econo JIT Compiler : The methods required only at run-time are compiled using this compiler and they are not stored for future use.
BCL stands for Base Class Library. It comprises classes, interface, and value types. It is the foundation for building .NET Framework applications, components, and controls.
FCL stands for Framework Class Library and is a collection of reusable types, including classes, interfaces, and data types included in the .NET Framework. It is used for developing a wide variety of applications, as it provides access to system functionality.
CTS stands for Common Type System. It follows a set of structured rules according to which a data type should be declared and used in the program code. It is used to describe all the data types that are going to be used in the application.
 
We can create our own classes and functions by following the rules in the CTS. It helps in calling the data type declared in one programming language by other programming languages.
Common Language Specification (CLS) helps the application developers to use the components that are inter-language compatible with certain rules that come with CLS. It also helps in reusing the code among all of the .NET-compatible languages.
Caching functionality in .NET Framework allows data storage in memory for rapid access. It helps improve performance by making data available, even if the data source is temporarily unavailable, and enhances scalability.
There are 3 types of caches in .NET :
 
* In-Memory Cache
* Persistent in-process Cache
* Distributed Cache
Cross-page posting is used to submit a form to a different page while creating a multi-page form to collect information from the user. You can specify the page you want to post to using the PostBackURL attribute.
MSIL is the abbreviation of Microsoft Intermediate Language. It is used to provide the instructions required for operations such as memory handling, exception handling, and more. It can also provide instructions to initialize and store values and methods easily.
The basic features of OOP are :
 
Encapsulation : Creation of self-contained modules that bind together the data and the functions that access that data.

Abstraction : Handles complexity and allows the implementation of further complex logic without disclosing it to the user object.

Polymorphism : Operation performed depends upon the context at runtime to facilitate easy integration.

Inheritance :  Creation of classes in a hierarchy to enable a class to inherit behavior from its parent class allowing reuse of code.
CAS stands for Code Access Security, CAS is a part of a security model that prevents unauthorized access to the resources. It also enables the users to set permissions for the code. CLR then executes the code depending upon the permissions.
 
CAS can only be used for managed code. If an assembly uses CAS it is treated as partially trusted. Although it goes through checks each time an assembly tries to access the resources.
ASP.NET introduces a concept of application domain or AppDomain which is like a lightweight process that acts like both container and boundary. The .NET run-time uses the AppDomain as a container for data and code. The CLR allows multiple .NET applications to run in a single AppDomain.
A delegate in .NET is similar to a function pointer in other programming languages like C or C++. A delegate allows the user to encapsulate the reference of a method in a delegate object. A delegate object can then be passed in a program, which will call the referenced method. We can even use a delegate method to create a custom event in a class.
Localization Globalization
It means changing the already globalized application to cater to a specific language or culture. Globalization is the process of developing applications to support multiple languages.
Microsoft.Extensions.Localization is used to localize the application content. Existing applications can also be converted to support multiple languages.
Managed Code Unmanaged Code
Managed by CLR Not managed by any entity
Garbage collection is used to manage memory Runtime environment takes care of the management
The .NET framework is necessary for the execution Not dependant on the .NET framework to run
There are four main steps that include in the execution of the managed code. They are as follows :
 
* Choosing a compiler that can execute the code written by a user
* Conversion of the code into Intermediate Language (IL) using a compiler
* IL gets pushed to CLR, which converts it into native code using JIT
* Native code is now executed using the .NET runtime
State management, as the name suggests, is used to constantly monitor and maintain the state of objects in the runtime. A web page or a controller is considered to be an object.
 
There are two types of state management :
 
Client-side : It is used to store information on the client’s machine and is formed mostly of reusable and simple objects.

Server-side : It stores the information on the server and makes it easier to manage and preserve the information on the server.
Interface Abstract Class
An interface merely declares a contract or behavior that implementing classes should have. An abstract class provides a partial implementation for a functionality that must be implemented by the inheriting entities.
An interface may declare only properties, methods and events with no access modifier. An abstract class declares fields too.
Stack Heap
Stored value type Stored reference type
A stack is responsible for keeping track of each executing thread and its location. The heap is responsible for keeping track of the more precise objects or data.
Client-side validation : When the validation takes place on the client-side browser, it is called client-side validation. Usually, JavaScript is used for client-side validation.
 
Server-side validation : When the validation takes place on the server then it is called server-side validation. Server-side validation is considered as a secure form of validation because even if the user bypasses the client-side validation we can still catch it in server-side validation.
In Server.Transfer page processing transfers from one page to the other page without making a round-trip back to the client’s browser. This provides a faster response with a little less overhead on the server. The clients url history list or current url Server does not update in case of Server.Transfer.
 
Response.Redirect is used to redirect the user’s browser to another page or site. It performs trip back to the client where the client’s browser is redirected to the new page. The user’s browser history list is updated to reflect the new address.
The advantages of Session State include :
 
* Easy to implement
* Stores user states and data across the application
* Ensures data durability
* Works in multi-process configuration, thereby ensuring platform scalability
* Stores session object on the server. Keeping it secure and transparent from the user
Inheritance is a crucial aspect of object-oriented programming that allows developers to create new classes that are built upon existing classes. This is achieved by reusing, extending, and modifying the behaviors defined in classes. The class whose members are inherited is the base class, while the class that inherits those members is called the derived class.
LINQ notices to Language Integrated Query. It was introduced with visual studio 2008 and is a uniform query syntax in C# and VB.NET for data retrieval from different sources and data manipulation irrespective of the data source.
Garbage collector frees the unused code objects in the memory. The memory heap is partitioned into 3 generations:
 
* Generation 0 : It holds short-lived objects.
* Generation 1 : It stores medium-lived objects.
* Generation 2 : This is for long-lived objects.

Collection of garbage refers to checking for objects in the generations of the managed heap that are no longer being used by the application. It also performs the necessary operations to reclaim their memory. The garbage collector must perform a collection in order to free some memory space.
 
During the garbage collection process :
 
* The list of live objects is recognized.
* References are updated for the compacted objects.
* The memory space occupied by dead objects is recollected. The remaining objects are moved to an older segment.

System.GC.Collect() method is used to perform garbage collection in .NET.
The main difference between managed and unmanaged code is listed below :
Managed Code Unmanaged Code
It is managed by CLR. It is not managed by CLR.
.NET framework is a must for execution. Does not require a .NET framework for the execution.
Memory management is done through garbage collection. Runtime environment takes care of memory management.
An assembly is a file that is automatically generated by the compiler which consists of a collection of types and resources that are built to work together and form a logical unit of functionality. We can also say, assembly is a compiled code and logical unit of code.
 
Assemblies are implemented in the form of executable (.exe) or dynamic link library (.dll) files.
Role-based security is used to implement security measures in .NET, based on the roles assigned to the users in the organization. In the organization, authorization of users is done based on their roles.
 
For example, windows have role-based access like administrators, users, and guests.
Assemblies are classified into 2 types. They are :
 
Private Assembly :
* It is accessible only to the application.
* We need to copy this private assembly, separately in all application folders where we want to use that assembly. Without copying, we cannot access the private assembly.
* It requires to be installed in the installation directory of the application.

Shared or Public Assembly :
* It can be shared by multiple applications.
* Public assembly does not require copying separately into all application folders. Only one copy of public assembly is required at the system level, we can use the same copy by multiple applications.
* It is installed in the Global Assembly Cache(GAC).
Following are the events in the page life cycle :
 
* Page_PreInit
 
* Page_Init
 
* Page_InitComplete
 
* Page_PreLoad
 
* Page_Load
 
* Page_LoadComplete
 
* Page_PreRender
 
* Render
Application Events : 
 
Application_Start, Application_End, Application_AcquireRequestState, Application_AuthenticateRequest, Application_AuthorizeRequest, Application_BeginRequest, Application_Disposed, Application_EndRequest, Application_Error, Application_PostRequestHandlerExecute, Application_PreRequestHandlerExecute, Application_PreSendRequestContent, Application_PreSendRequestHeaders, Application_ReleaseRequestState, Application_ResolveRequestCache, Application_UpdateRequestCache

Session Events : 
 
Session_Start, Session_End
Boxing is the process of converting a value type into a reference type directly. Boxing is implicit.
 
Unboxing is the process where reference type is converted back into a value type. Unboxing is explicit.
 
An example is given below to demonstrate boxing and unboxing operations :
int a = 10;      // a value type
object o = a;     // boxing
int b = (int)o;   // unboxing
We can use the appSettings block in the web.config file, if we want to set the user-defined values for the whole application. Example code given below will make use of ConnectionString for the database connection throughout the project :
<em>
   <configuration>
       <appSettings>
           <add key= "ConnectionString" value="server=local; pwd=password; database=default"  />
       </appSettings>
   </configuration>
</em>
The For loop provides a concise way of writing the loop structure, but the While loop is a control flow statement that allows repetitive execution of the code. Initialization, condition checking, iteration statements are written at the top of the For loop, but only initialization and condition checking is done at the top of the while loop.
 
Syntax :
 
While loop :
while(condtion) {

//statements to excute.

}
 
For loop :
for(intialization; condition; Increment or decrement){

// statements to be excuted.

}
Function Stored Procedure
Can only return one value Can return any number of values
No support for exception handling using try-catch blocks Supports the usage of try-catch blocks for exception handling
The argument consists of only one input parameter Both input and output parameters are present
A function can be called from a stored procedure The stored procedure cannot be called from a function
Manifest is mainly used to store the metadata of the assembly. It contains a variety of metadata which is required for many things as given below :
 
* Assembly version information
* Security identification
* Scope checking of the assembly
* Reference validation of classes
Memory-mapped files in .NET are used to instantiate the contents of a logical file into the address of the application. This helps a user run processes simultaneously on a single machine and have the data shared between processes.
 
The MemoryMappedFile.CreateFromFiles() function is used to obtain a memory-mapped file object easily.
GAC Stands for Global Assembly Cache. GAC is a part of CLR, which is used to store the assemblies that are shared across all of the applications. A user can make use of the Gacutil application to add any file into GAC easily.
Two types of memories are present in .NET. They are :
 
Stack : Stack is a stored-value type that keeps track of each executing thread and its location. It is used for static memory allocation.
 
Heap : Heap is a stored reference type that keeps track of the more precise objects or data. It is used for dynamic memory allocation.
MDI (Multiple Document Interface) : An MDI allows you to open multiple windows, it will have one parent window and as many child windows. The components are shared from the parent window like toolbar, menubar, etc.
 
SDI (Single Document Interface) : SDI opens each document in a separate window. Each window has its own components like a toolbar, menubar, etc. Therefore it is not constrained to the parent window.
WebHostBuilder function is used for HTTP pipeline creation through webHostBuilder.Use() chaining all at once with WebHostBuilder.Build() by using the builder pattern. This function is provided by Microsoft.AspNet.Hosting namespace. The Build() method’s purpose is building necessary services and a Microsoft.AspNetCore.Hosting.IWebHost for hosting a web application.
IGCToCLR interface will be passed as an argument to the InitializeGarbageCollector() function and it is used for runtime communication. It consists of a lot of built-in methods such as RestartEE(), SuspendEE(), etc.
.NET Core Mono
.Net Core is the subset of implementation for the .NET framework by Microsoft itself. Mono is the complete implementation of the .Net Framework for Linux, Android, and iOS by Xamarin.
.NET Core only permits you to build web applications and console applications. Mono permits you to build different application types available in .NET Framework, including mobile applications, GUI-enabled desktop apps, etc.
.NET Core does not have the built-in capability to be compiled into WebAssembly-compatible packages. Mono has the built-in capability to be compiled into WebAssembly-compatible packages.
.NET Core is never intended for gaming. You can only develop a text-based adventure or relatively basic browser-based game using .NET Core. Mono is intended for the development of Games. Games can be developed using the Unity gaming engine that supports Mono.
It is a component that allows the publishing of the application's function on the web to make it accessible to the public. It resides on a Web server and provides information and services using standard Web protocols such as HTTP and Simple Object Access Protocol (SOAP).
The advantages of Web Services are :
 
* It is simple to build and supported by a variety of platforms.
* It can extend its interface and add new methods without affecting the client's operations.
* It is stateless and firewall-friendly.
Tuples are data structures that hold object properties and contain a sequence of elements of different data types. They were introduced as a Tuple<T> class in .NET Framework 4.0 to avoid the need of creating separate types to hold object properties.
PE stands for Portable Executable. It is a derivative of the Microsoft Common Object File Format (COFF).  Windows executable. EXE or DLL files follow the PE file format. It consists of four parts:
 
 1. PE/COFF headers- Contains information regarding. EXE or DLL file.
 
2. CLR header- Contains information about CLR & memory management.
 
3. CLR data- Contains metadata of DDLs and MSIL code generated by compilers.
 
4. Native image section- Contains sections like .data, .rdata, .rsrc, .text etc.
Multi-threading is a process that contains multiple threads each of which performs different activities within a single process. .NET supports multithreading in two ways :
 
* Starting threads with ThreadStart delegates.

* Using the ThreadPool class with asynchronous methods.
AutoPostBack is a property in the .NET framework, which provides automatic postback whenever an event starts its execution cycle. To use AutoPostBack, you have to set the property to True.
Every request into an ASP.NET application is handled by a specialized component called HTTP handler. It is the most important component for handling ASP.NET application requests.
 
It uses different handlers to serve different files. The handler for web page creates the page and control objects, runs your code and then renders the final HTML.
 
Following are the default HTTP handlers for ASP.NET :
 
Page Handler(.aspx) : Handles web pages
 
User Control Handler(.ascx) : It handles web user control pages
 
Web Service Handler(.asmx) : Handles web service pages
 
Trace Handler(trace.axd) : It handles trace functionality
ExecuteScalar ExecuteNonQuery
Returns the output value Does not return any value
Used for fetching a single value Used to execute insert and update statements
Does not return the number of affected rows Returns the number of affected rows.