Dotnet Concepts
.NET Concepts
1. Namespace
A namespace is a container that holds classes, interfaces, structs, enums, and other namespaces.
Purpose:
- Logically group related classes and types
- Avoid naming conflicts
- Improve code readability and maintainability
Common Use Cases:
- Organizing layers: Models, Services, Controllers
- Separating business logic from data access
- Grouping helper methods or extensions
Key Points:
- Use
namespacekeyword - Supports nested namespaces
- Reference with
usingdirective - Namespace can span multiple files
Types:
- System: Built-in (e.g.,
System.Linq) - User-defined: e.g.,
MyApp.Services
2. GC (Garbage Collector)
The Garbage Collector (GC) in .NET automatically reclaims memory used by unreferenced objects.
How It Works:
- CLR tracks object references
- When needed, GC reclaims unused memory
- Runs in background or on low memory triggers
Triggering Conditions:
- Memory usage crosses a threshold
- Explicit call:
GC.Collect()(not recommended) - App is idle or background threads free
3. CLR (Common Language Runtime)
The CLR is the runtime engine that manages the execution of .NET programs.
Responsibilities:
- Memory management via GC
- JIT Compilation and execution
- Exception handling
- Thread management & security
GC vs CLR Comparison
| Feature | GC (Garbage Collector) | CLR (Common Language Runtime) |
|---|---|---|
| Role | Manages memory | Executes .NET applications |
| Scope | Part of CLR | Entire runtime environment |
| Manual Control | Minimal (GC.Collect()) |
None |
| Developer Involvement | None required | Write managed code |
4. Variable vs Field vs Property
| Concept | Scope/Location | Usage | Access Control |
|---|---|---|---|
| Variable | Inside method/block | Temporary data | Local only |
| Field | Inside class/struct | Stores object state | Accessible in class |
| Property | Inside class (with accessors) | Encapsulates field | Controlled access (get/set) |
public int Age { get; set; }
5. Enum in C#
Enum defines a set of named constants backed by integers.
enum Status { Active, Inactive, Suspended }
Status userStatus = Status.Active;
Pros:
- Improves code readability and type safety
- Works well with switch-case
- Supports metadata via attributes
Cons:
- No methods or behavior
- Static and limited to constants
- Not ideal for dynamic lists
6. Reflection in C#
Reflection allows runtime access to metadata, types, methods, and more.
var type = typeof(MyClass);
var methods = type.GetMethods();
foreach (var method in methods)
{
Console.WriteLine(method.Name);
}
Pros:
- Dynamic behavior
- Frameworks, DI containers, and tools rely on it
Cons:
- Slower performance
- Runtime errors possible
- Can expose internals
Enum vs Reflection
| Feature | Enum | Reflection |
|---|---|---|
| Purpose | Define fixed constants | Inspect metadata dynamically |
| Type | Value type | API: System.Reflection |
| Use Case | Status codes, roles | Metadata, dynamic access |
7. Task, async, await in C#
Task represents an asynchronous operation.
async marks a method as asynchronous.
await waits for the task without blocking.
public async Task<string> GetDataAsync()
{
await Task.Delay(1000);
return "Hello World";
}
Use Cases:
- Database calls
- HTTP requests
- File I/O
- Sending emails
8. const vs readonly
| Feature | const | readonly |
|---|---|---|
| Set Time | Compile-time | Runtime (constructor) |
| Modifiable | No | Once during initialization |
| Scope | Always static | Can be instance-level |
| Type | Primitive/string | Any type |

Comments
Post a Comment