Skip to main content
InterviewHubPRO
Section Overview
Q27

Question 27 of 50

Part 3: CLR Internals, Native AOT & Production Diagnostics (25 - 35)

54%
C# & CLR Internals (50 Qs)•Part 3: CLR Internals, Native AOT & Production Diagnostics (25 - 35)

How does Unsafe code, stackalloc, and P/Invoke interop work in C#?

senior LevelCode ExampleMemory ManagementData Structures

In Simple Words: C# supports pointer arithmetic within unsafe code blocks. stackalloc allocates memory directly on the execution stack rather than the managed heap, requiring zero garbage collection.

csharp
Span<int> numbers = stackalloc int[128]; // Instant stack allocation!

P/Invoke (DllImport) allows C# to invoke native C/C++ OS system libraries directly.