Top .NET Interview Questions and Answers
What is .NET?
.NET is a software framework developed by Microsoft for building and running various applications (web, desktop, mobile). It provides a runtime environment (Common Language Runtime or CLR), a vast class library, and tools to simplify development. The framework allows for cross-language interoperability, meaning you can use different programming languages (C#, VB.NET, F#) within the same application.
Programming Languages Supported by .NET
.NET supports a wide range of programming languages, including C#, VB.NET, F#, and others. The exact number may vary depending on the specific .NET version.
.NET Language Support
.NET's ability to support multiple languages stems from its use of the Common Language Runtime (CLR). Languages compile to a common intermediate language (MSIL, or Common Intermediate Language), enabling them to interact seamlessly within the .NET environment.
ASP.NET vs. ASP (Active Server Pages)
ASP.NET | ASP (Active Server Pages) |
---|---|
Compiled; object-oriented; uses ADO.NET. | Interpreted; partially object-oriented; uses ADO. |
State Management in ASP.NET
State management is essential for maintaining data across multiple requests in web applications. ASP.NET offers both client-side (using cookies, browser cache) and server-side (using session state, application state, profile properties) techniques.
Debug vs. Trace in .NET
Debug |
Trace |
---|---|
Used for debugging builds only; statements are removed in release builds. | Used for both debug and release builds; allows for monitoring application behavior in production. |
StringBuilder
vs. String
in .NET
StringBuilder |
String |
---|---|
Mutable (can be changed after creation). More efficient for string manipulation. | Immutable (cannot be changed after creation). Less efficient for repeated concatenation. |
int
vs. Int32
in .NET
int
and Int32
are aliases for the same data type (a 32-bit signed integer).
Namespace vs. Assembly in .NET
Namespace | Assembly |
---|---|
Logical grouping of classes; provides a naming hierarchy. | Physical grouping of code (DLL or EXE file). |
Value Type vs. Reference Type in .NET
Value Type | Reference Type |
---|---|
Stores data directly; memory allocated on the stack. | Stores a reference to an object; memory allocated on the heap. |
Examples: int , float , struct |
Examples: class , string , array |
Session vs. Application Objects in ASP.NET
Session Object | Application Object |
---|---|
Stores data specific to a user's session; data is lost when the session ends. | Stores data shared across all users of the application; data persists for the application's lifetime. |
Functions vs. Stored Procedures in .NET
Function | Stored Procedure |
---|---|
Returns a single value; can be used in SELECT statements. |
Can return multiple values; cannot be used in SELECT statements. |
Has only input parameters. | Can have input and output parameters. |
Retrieving User Name with Windows Authentication
Syntax (C#)
string userName = System.Environment.UserName;
Hashtable vs. ArrayList in .NET
Hashtable | ArrayList |
---|---|
Stores key-value pairs. | Stores a list of values. |
Access values using keys. | Access values using index. |
Can store different data types. | Typically stores only one data type. |
Immutability in .NET
An immutable object cannot be modified after it's created. Any operation that appears to modify it actually creates a new object.
Advantages of Using Sessions in ASP.NET
- Stores data specific to each user's session.
- Easy to implement and use.
- Secure (server-side storage).
Disadvantages of Using Sessions in ASP.NET
- Performance overhead with many users (server-side memory usage).
- Serialization/deserialization overhead.
Setting Session Timeout
You can configure session timeout settings in the web.config file.
Boxing and Unboxing in .NET
Boxing | Unboxing |
---|---|
Converting a value type to a reference type. | Converting a reference type to a value type. |
Modifying Primary Key Index
Generally, you cannot modify the index of a primary key column in a database after the table is created. The primary key must always remain unique and not contain nulls.
HTTPHandler in ASP.NET
An HTTP handler is a class that processes incoming HTTP requests in ASP.NET, providing a low-level mechanism to handle requests.
.NET Framework Components
- Class Library (Framework Class Library or FCL): Provides a vast collection of reusable classes and types.
- Common Language Runtime (CLR): Manages the execution of .NET applications.
- Dynamic Language Runtime (DLR): Supports dynamic languages in .NET.
- Other components (like Application Domains, Runtime Hosts, security features).
Manifest Files in .NET
A manifest file contains metadata about an assembly (a .NET component). This metadata is essential for the CLR to load and manage the assembly.
Memory-Mapped Files
Memory-mapped files map a file's contents into an application's address space, allowing for efficient access to the file's data.
Enforcing Garbage Collection
Use System.GC.Collect()
to trigger garbage collection, though it's generally best to let the garbage collector manage memory automatically.
Dispose()
vs. Finalize()
Dispose() |
Finalize() |
---|---|
Explicitly called by the developer to release unmanaged resources. | Called by the garbage collector to release unmanaged resources. |
Code Access Security (CAS)
CAS is a .NET security feature that controls the permissions granted to code, limiting what resources it can access and operations it can perform.
Garbage Collection
Garbage collection is an automatic memory management process that reclaims memory occupied by objects no longer in use.
Checking for Postbacks in ASP.NET
Use the IsPostBack
property to determine whether the current request is a postback or an initial request.
Variables and Constants in .NET
Variable | Constant |
---|---|
Holds a value that can change. | Holds a value that cannot change after initialization. |
Declared using a data type (e.g., int myVar = 10; ). |
Declared using const (e.g., const int MY_CONSTANT = 10; ). |
Replacing Multiple if-else
Statements
Use a switch
statement (C#) or Select Case
statement (VB.NET).
Types of Indexes in .NET
- Clustered index
- Non-clustered index
.NET Memory Types
- Stack memory
- Heap memory
New Features in .NET Framework 4.0
- Improved application compatibility and deployment.
- Dynamic Language Runtime (DLR).
- Managed Extensibility Framework (MEF).
- Parallel programming support.
- Enhanced security model.
.NET Framework Enhancements
- Networking Improvements: Enhanced networking capabilities and performance.
- Core ASP.NET Services: Improvements to core ASP.NET services for web application development.
- WPF (Windows Presentation Foundation) 4 Improvements: Enhancements to WPF for creating rich user interfaces.
- Entity Framework (EF) Enhancements: Improvements to EF for data access.
- WCF (Windows Communication Foundation) and WF (Windows Workflow Foundation) Integration: Better integration between these technologies for building distributed applications.
Cookies
Cookies are small pieces of data stored on a user's computer by a web server. They are often used to store session information or user preferences. Cookies are sent to the browser as HTTP headers.
Disadvantages of Cookies
- Limited storage capacity (only strings).
- Browser-dependent (behavior varies across browsers).
- Security risks (if not handled properly).
IL (Intermediate Language)
IL (Intermediate Language), also known as MSIL (Microsoft Intermediate Language) or CIL (Common Intermediate Language), is the language generated by the .NET compiler. It is a platform-independent intermediate representation that's executed by the CLR (Common Language Runtime) either by interpretation or by using a JIT (Just-In-Time) compiler.
Enforcing Garbage Collection
C# Syntax
System.GC.Collect();
Tuples in .NET
Tuples are lightweight data structures that can hold a fixed number of elements of potentially different data types. They are useful for returning multiple values from a method.
Tuple Size Limits
A tuple in .NET can hold up to 8 elements. If you need more, you can nest tuples.
Dataset Architecture
A dataset in .NET follows a disconnected architecture, meaning it's not directly connected to the database. This allows for offline data manipulation.
Checking DataReader Status
Use the DataReader.IsClosed
property to check if a DataReader
is open or closed.
Connection Pooling Requirements
- Multiple processes need to share the same connection.
- The connection string must be identical.
Adapter for Access Databases
Use OleDbDataAdapter
to retrieve data from Microsoft Access databases.
Connection Pooling Parameters
Connect Timeout
Max Pool Size
Min Pool Size
Pooling
(enabled/disabled)
AutoPostBack
AutoPostBack
is a property of web controls that automatically posts back to the server when an event occurs (e.g., a button click).
Binding a DataGridView Control
Use the DataSource
and DataMember
properties to bind a DataGridView control to a data source.
WCF (Windows Communication Foundation)
WCF is a framework for building service-oriented applications in .NET. It's used to create and consume web services.