Top 10 .Net Interview Questions
1. What is the .NET Framework?
The .NET Framework is a software development platform by Microsoft that provides a controlled environment for developing, installing, and running Windows-based apps. It includes two key components: the Common Language Runtime (CLR) that manages program execution, and the .NET Framework Class Library (FCL), a set of reusable classes, libraries, and APIs.
2. What is the difference between .NET Core and .NET Framework?
.NET Framework supports only Windows-based apps, whereas .NET Core is cross-platform, open-source, and works on Windows, macOS, and Linux. .NET Core is modern, flexible, and suited for developing apps on multiple platforms, unlike .NET Framework, which is confined to Windows.
3. What is the Common Language Runtime (CLR)?
The CLR is the virtual machine that manages the execution of .NET programs. It handles key services like memory management, garbage collection, and exception handling, allowing developers to focus on business logic rather than low-level concerns.
4. What is Managed Code?
Managed code is code that runs under the control of the CLR, which provides services such as garbage collection, memory management, and type safety. Unmanaged code, by contrast, runs outside of the CLR and directly on the OS.
5. What is the Global Assembly Cache (GAC)?
The GAC is a repository used to store shared assemblies across multiple .NET applications. Assemblies in the GAC must have a strong name, which includes the assembly’s version, public key, and culture info. This ensures version control and prevents conflicts between shared libraries.
6. What are Value Types and Reference Types?
Value types (e.g., int, char) store the actual data and are allocated on the stack, while reference types (e.g., objects, arrays) store a reference to the data, which resides in the heap. Value types are faster for small data, while reference types handle complex data structures.
7. What is an Assembly?
An assembly is a compiled code library in .NET. It can be private (used by a single app) or shared (available to multiple apps via the GAC). Assemblies include compiled code, resources, and metadata. They help modularize applications and manage versioning.
8. What is the difference between String and StringBuilder?
String is immutable, meaning modifications create a new instance, while StringBuilder is mutable and allows in-place changes. For frequent string manipulations, StringBuilder is more efficient.
9. What is garbage collection in .NET?
Garbage collection (GC) is an automated process in .NET that frees memory by reclaiming objects no longer in use. The CLR manages this in three generations (Gen 0, Gen 1, Gen 2) for efficient memory management.
10. What is Dependency Injection (DI) in .NET Core?
Dependency Injection is a design pattern that allows you to inject dependencies into a class rather than creating them within the class. This makes classes more flexible and loosely coupled.