Dot Net TechnologyUnit 19 min read
.NET Framework & C: Architecture, CLR, IL, JIT, and C Basics
Unit 1 of Dot Net Technology covers the foundational architecture of .NET Framework (CLR, JIT, MSIL), C language features (data types, operators, control structures), and how .NET executes programs—with visual traces of compilation, memory management, and code execution.
TAKEAWAYS:
- .NET Framework is a runtime environment (CLR + libraries) that executes managed code via JIT compilation of MSIL (Intermediate Language).
- C# is a statically typed, object-oriented language that compiles to MSIL and runs under CLR (Common Language Runtime).
- CLR handles memory management (garbage collection), security, and thread execution for .NET applications.
- JIT (Just-In-Time) Compiler converts MSIL to native machine code at runtime for performance.
- C# syntax includes
class,namespace,Main(), and strict typing (e.g.,int,string,bool). - Exam focus: Compare .NET vs. Java, trace CLR execution steps, and write C# programs with proper structure.
1. .NET Framework Architecture
The .NET Framework is Microsoft’s platform for building and running applications. It consists of:
- CLR (Common Language Runtime): Executes managed code, handles memory, security, and threading.
- FCL (Framework Class Library): Pre-built reusable libraries (e.g.,
System,System.Collections). - Languages: C#, VB.NET, F# (all compile to MSIL).
How .NET Executes Code
- Source Code (e.g.,
Program.cs) → Compiler (e.g.,csc.exe) → MSIL (Intermediate Language). - MSIL is stored in an assembly (
.exeor.dll). - CLR loads the assembly and uses the JIT Compiler to convert MSIL to native machine code.
- Execution: Native code runs on the CPU, while CLR manages memory (garbage collection) and exceptions.
flowchart TD
A["Source Code<br/>(Program.cs)"] -->|"C# Compiler"| B["MSIL<br/>(Intermediate Language)"]
B -->|"CLR Loader"| C["JIT Compiler"]
C -->|"Native Code"| D["CPU Execution"]
D -->|"CLR"| E["Garbage Collection<br/>Exception Handling"]Key Components of CLR
| Component | Role |
|---|---|
| JIT Compiler | Converts MSIL to machine code at runtime. |
| Garbage Collector | Automatically frees unused memory (no manual free() like in C). |
| Type System | Enforces strong typing (e.g., int vs. string). |
| Security | Validates code before execution (e.g., sandboxing). |
2. C# Language Basics
C# is a statically typed, object-oriented language designed for .NET. Key features:
- Case-sensitive:
MyClass≠myclass. - Strong typing: Variables must declare types (
int x = 5;). - Namespace: Organizes code (e.g.,
using System;). - Entry point:
Main()method (static, void).
Structure of a C# Program
using System; // Namespace for libraries
namespace MyApp // Logical grouping
{
class Program // Class definition
{
static void Main(string[] args) // Entry point
{
Console.WriteLine("Hello, .NET!"); // Output
}
}
}
Data Types in C#
| Category | Types | Example |
|---|---|---|
| Value Types | int, float, bool, char |
int age = 25; |
| Reference Types | string, class, array |
string name = "Alice"; |
| Special | object, dynamic |
object x = 10; |
Example: Variable Declaration
int number = 100; // Value type (stack)
string text = "C#"; // Reference type (heap)
object anyType = number; // Can hold any type
3. Memory Management in .NET
Unlike C/C++, .NET uses garbage collection (GC) to manage memory:
- Stack: Stores value types and method calls (fast access).
- Heap: Stores reference types (objects, arrays).
- GC: Automatically frees unreachable objects.
[Stack] [Heap]
+--------+ +--------+
| int x | | Object |
+--------+ +--------+
| Method | --> | Ref |
+--------+ +--------+
- Stack: Fixed-size, LIFO (Last-In-First-Out).
- Heap: Dynamic, GC-managed.
Example: Object Lifetime
class Program {
static void Main() {
var obj = new MyClass(); // Allocated on heap
obj = null; // Eligible for GC
}
}
After obj = null:
[Heap]
+--------+
| MyClass| ← Unreachable (GC collects)
+--------+
4. Compilation and Execution Flow
Step-by-Step Trace
| Step | Action |
|---|---|
| 1. Source Code | Program.cs written in C#. |
| 2. Compilation | csc.exe Program.cs → Program.exe (contains MSIL). |
| 3. CLR Load | Program.exe loaded into CLR. |
| 4. JIT Compile | MSIL → Native code (e.g., Main() method). |
| 5. Execution | Native code runs on CPU; CLR handles memory/exceptions. |
5. C# Control Structures
Conditional Statements
if (age >= 18) {
Console.WriteLine("Adult");
} else {
Console.WriteLine("Minor");
}
Trace Table:
age |
Condition (age >= 18) |
Output |
|---|---|---|
| 20 | true |
"Adult" |
| 15 | false |
"Minor" |
Loops
// For loop
for (int i = 0; i < 5; i++) {
Console.WriteLine(i);
}
// While loop
int j = 0;
while (j < 5) {
Console.WriteLine(j);
j++;
}
In the Real World
eSewa (Nepal)
- Uses .NET Core for backend services (e.g., payment processing).
- CLR manages concurrent transactions (e.g., mobile top-ups) safely.
- LINQ queries user data in databases (e.g.,
from user in Users where user.Status == "Active" select user).
Khalti (Digital Payments)
- ASP.NET Core handles API requests (e.g.,
POST /payments). - JIT Compiler optimizes payment validation logic for speed.
- Garbage Collection prevents memory leaks during high traffic.
- ASP.NET Core handles API requests (e.g.,
NTC (Nepal Telecom) Billing System
- ADO.NET (disconnected architecture) fetches customer data from SQL Server.
- C#
try-catchhandles exceptions (e.g., failed database connections). - Example: A
BillGeneratorclass usesSqlConnectionto query usage records:using (SqlConnection con = new SqlConnection(connectionString)) { con.Open(); SqlCommand cmd = new SqlCommand("SELECT * FROM Usage WHERE CustomerID = @id", con); cmd.Parameters.AddWithValue("@id", customerId); SqlDataReader reader = cmd.ExecuteReader(); // Process data... }
6. Comparing .NET and Java
| Feature | .NET Framework | Java |
|---|---|---|
| Language | C#, VB.NET | Java |
| Runtime | CLR | JVM (Java Virtual Machine) |
| Compilation | MSIL → JIT → Native Code | Bytecode → JIT → Native Code |
| Memory Mgmt | Garbage Collection (GC) | Garbage Collection (GC) |
| Platform | Windows-focused (but cross-platform with .NET Core) | Cross-platform |
| Libraries | FCL (e.g., System.Collections) |
JDK (e.g., java.util) |
Key Difference:
- .NET is tightly integrated with Windows (historically), while Java is platform-agnostic.
7. Common Pitfalls and Best Practices
- Avoid
using staticOveruse- Example:
using static System.Console;lets you callWriteLine()directly, but can cause naming conflicts.
- Example:
- Dispose Unmanaged Resources
- Use
IDisposablefor objects likeSqlConnection:using (var con = new SqlConnection(connectionString)) { // Safe disposal after block }
- Use
- Prefer
varfor Local Variables- Cleaner code when type is obvious:
var list = new List<int>(); // Compiler infers type
- Cleaner code when type is obvious:
Exam Tip
- Diagrams Are Key
- Draw CLR execution flow (source → MSIL → JIT → native code).
- Show stack vs. heap memory allocation for objects.
- Code Structure Matters
- Always include:
usingdirectives.namespaceandclass.Main()method.
- Example starter:
using System; namespace MyApp { class Program { static void Main() { // Your code here } } }
- Always include:
- Trace Tables for Algorithms
- For loops/conditions, show a table of variable states (like the
ifexample above).
- For loops/conditions, show a table of variable states (like the
- Real-World Scenarios
- Link concepts to eSewa (transactions), Khalti (APIs), or NTC (database queries).
- Example question: "How does CLR ensure thread safety in a banking app like Nabil Bank’s online service?" Answer: CLR’s garbage collection and memory isolation prevent race conditions in multi-threaded transactions.
Visual Summary
mindmap
root((.NET Framework))
CLR
JIT Compiler
Garbage Collection
Type System
C#
Syntax
Data Types
Control Structures
Memory
Stack
Heap
Execution Flow
Source → MSIL → Native CodeBased on the TU BCA syllabus for Dot Net Technology (CACS408), unit 1.
Discussion
Loading…