Dotnet Concepts

.NET Concepts


1. Namespace

A namespace is a container that holds classes, interfaces, structs, enums, and other namespaces.

Purpose:

Common Use Cases:

Key Points:

  • Use namespace keyword
  • Supports nested namespaces
  • Reference with using directive
  • Namespace can span multiple files

Types:

2. GC (Garbage Collector)

The Garbage Collector (GC) in .NET automatically reclaims memory used by unreferenced objects.

How It Works:

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:

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:

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:

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

Popular posts from this blog

Debouncing & Throttling in RxJS: Optimizing API Calls and User Interactions

Promises in Angular

Comprehensive Guide to C# and .NET Core OOP Concepts and Language Features