.NET ProgrammingUnit 17 min read
.NET Framework: Architecture, CLR, BCL, and Applications
Unit 1 of .NET Programming introduces the .NET Framework—its architecture, Common Language Runtime (CLR), Base Class Library (BCL), and real-world applications in Nepalese software (e.g., eSewa, Khalti). Learn how .NET enables cross-platform development, memory management, and integration with databases.
1. What is .NET Framework?
.NET Framework is a software development platform by Microsoft that provides:
- A runtime environment (CLR) to execute programs.
- A class library (BCL) for reusable code (e.g., file I/O, networking).
- Language interoperability (C#, VB.NET, F# can work together).
- Cross-platform support (via .NET Core and .NET 5+).
Key Components
| Component | Role |
|---|---|
| CLR (Common Language Runtime) | Manages memory, executes code, enforces security. |
| BCL (Base Class Library) | Pre-built classes for common tasks (e.g., System.IO, System.Collections). |
| FCL (Framework Class Library) | Extends BCL with domain-specific libraries (e.g., System.Web). |
| CIL (Common Intermediate Language) | Compiled code before execution (JIT-compiled to machine code). |
2. How .NET Works: Compilation and Execution
.NET programs follow this workflow:
- Source Code (e.g.,
Program.csin C#) → Compiler → CIL (MSIL). - JIT Compiler converts CIL to machine code at runtime.
- CLR executes the code, handles memory, and manages threads.
Visual: Compilation Process
flowchart LR
A["Source Code<br/>(e.g., C#)"] -->|"Compiler"| B["CIL<br/>(MSIL)"]
B -->|"JIT Compiler"| C["Machine Code"]
C -->|"CLR"| D["Execution<br/>(Memory Management, Threads)"]Example: A Simple C# Program
using System;
class Program {
static void Main() {
Console.WriteLine("Hello, .NET!");
}
}
Trace:
| Step | Action | Output/State |
|---|---|---|
| 1 | Program.cs compiled |
Generates Program.exe (CIL) |
| 2 | CLR loads Program.exe |
JIT compiles Main() to machine code |
| 3 | Main() executes |
Prints "Hello, .NET!" |
3. CLR: The Engine Behind .NET
The CLR handles:
- Memory Management: Garbage Collection (GC) automatically frees unused objects.
- Security: Code Access Security (CAS) restricts unsafe operations.
- Threading: Manages multi-threaded applications.
- Exception Handling: Structured error handling via
try-catch-finally.
Garbage Collection (GC) in Action
sequenceDiagram
participant App as Application
participant CLR as CLR
participant GC as Garbage Collector
App->>CLR: Allocates Object A
App->>CLR: Loses Reference to A
CLR->>GC: Request GC
GC->>CLR: Frees Object AExample: GC in C#
class Program {
static void Main() {
var obj = new Object(); // Allocated on heap
obj = null; // No references → GC target
GC.Collect(); // Forces collection (rarely used)
}
}
4. BCL: Reusable Libraries
The BCL provides classes for:
- File I/O:
System.IO(e.g.,File.ReadAllText()). - Collections:
List<T>,Dictionary<TKey, TValue>. - Networking:
System.Net(e.g., HTTP requests). - Multithreading:
System.Threading(e.g.,Task).
Example: Reading a File (BCL in Action)
using System;
using System.IO;
class Program {
static void Main() {
string text = File.ReadAllText("example.txt");
Console.WriteLine(text);
}
}
Trace:
| Step | Action | Output/State |
|---|---|---|
| 1 | File.ReadAllText() called |
Opens "example.txt" |
| 2 | Reads entire file | Returns string content |
| 3 | Prints content | Displays file text |
5. .NET Versions: Framework vs. Core vs. 5+
| Version | Key Features | Use Case |
|---|---|---|
| .NET Framework | Windows-only, tightly integrated with Windows. | Legacy desktop apps (WinForms). |
| .NET Core | Cross-platform, modular, high performance. | Microservices, APIs. |
| .NET 5+ | Unified (Framework + Core), single runtime. | Modern apps (Blazor, MAUI). |
6. Real-World Applications in Nepal
1. eSewa (Digital Payments)
- Uses: .NET Core for backend APIs (e.g., transaction processing).
- How: CLR manages concurrent requests; BCL handles encryption (
System.Security.Cryptography).
2. Khalti (Mobile Banking)
- Uses: .NET Framework for Windows-based admin dashboards.
- How: BCL’s
System.Dataconnects to SQL Server for user data.
3. Daraz (E-Commerce)
- Uses: .NET 5+ for scalable web APIs.
- How: CLR’s garbage collection prevents memory leaks during high traffic.
4. NTC (Telecom Billing)
- Uses: .NET Framework for legacy billing systems.
- How: BCL’s
System.Threadinghandles parallel call processing.
7. Advantages and Disadvantages of .NET
| Advantages | Disadvantages |
|---|---|
| Cross-platform (Core/5+). | Steeper learning curve for beginners. |
| Strong typing and safety (CLR checks). | Licensing costs for enterprise use. |
| Large BCL for rapid development. | Performance overhead vs. native code. |
| Integration with Azure cloud. | Limited open-source ecosystem (vs. Java). |
8. Exam Tip: What to Focus On
- Architecture: Know CLR vs. BCL vs. FCL roles.
- Compilation: Source → CIL → JIT → Execution.
- GC: How memory is managed (e.g.,
GC.Collect()). - BCL Examples:
System.IO,System.Collections. - Version Differences: When to use Framework vs. Core.
- Real-World Links: Connect concepts to eSewa/Khalti/Daraz.
Common Exam Questions:
- "Explain the role of CLR in .NET."
- "How does garbage collection work in C#?"
- "Compare .NET Framework and .NET Core."
Summary Table for Quick Review
| Concept | Key Idea |
|---|---|
| CLR | Runtime engine (memory, security, threading). |
| BCL | Pre-built libraries (e.g., System.IO). |
| CIL | Intermediate code before JIT compilation. |
| GC | Automatic memory cleanup. |
| .NET Core | Cross-platform, modular runtime. |
| JIT | Converts CIL to machine code at runtime. |
Based on the TU BITM syllabus for .NET Programming (IT275), unit 1.
Discussion
Loading…