What is unmanaged code in C#?

Unmanaged code in C# refers to code that executes outside the control of the .NET Common Language Runtime (CLR). This type of code is also known as unsafe code because it bypasses many of the safety mechanisms that make C# a managed language.

In C#, unmanaged code typically involves the use of pointers and direct memory manipulation, similar to languages like C and C++. To use unmanaged code, you must explicitly mark code blocks with the unsafe keyword.

Key Characteristics of Unmanaged Code

  • Applications that are not under the control of the CLR are unmanaged.

  • The unsafe code or the unmanaged code is a code block that uses a pointer variable.

  • The unsafe modifier allows pointer usage in unmanaged code.

  • Memory management becomes the programmer's responsibility.

Managed vs Unmanaged Code Managed Code ? CLR controlled ? Automatic GC ? Type safety ? No pointers ? Safe execution Unmanaged Code ? Direct memory access ? Manual memory mgmt ? Pointer usage ? unsafe keyword ? Higher performance Use unmanaged code only when necessary for performance or interoperability

Syntax

To declare unsafe code blocks, use the unsafe modifier −

unsafe {
    // pointer operations here
    int* ptr = &variable;
}

For unsafe methods, apply the unsafe modifier to the entire method −

static unsafe void MethodName() {
    // unsafe operations
}

Using Pointers in Unsafe Code

Here is an example showing how to declare and use a pointer variable. We have used the unsafe modifier here −

Example

using System;

class Program {
    static unsafe void Main(string[] args) {
        int var = 20;
        int* p = &var;

        Console.WriteLine("Data is: {0} ", var);
        Console.WriteLine("Address is: {0}", (long)p);
        Console.WriteLine("Value at address: {0}", *p);
    }
}

The output of the above code is −

Data is: 20 
Address is: 140728898994684
Value at address: 20

Pointer Arithmetic Example

Example

using System;

class Program {
    static unsafe void Main(string[] args) {
        int[] numbers = {10, 20, 30, 40, 50};
        
        fixed (int* ptr = numbers) {
            for (int i = 0; i < 5; i++) {
                Console.WriteLine("numbers[{0}] = {1}", i, *(ptr + i));
            }
        }
    }
}

The output of the above code is −

numbers[0] = 10
numbers[1] = 20
numbers[2] = 30
numbers[3] = 40
numbers[4] = 50

When to Use Unmanaged Code

Use Case Description
Performance Critical Operations Direct memory access can be faster than managed alternatives
Interoperability Calling native APIs or working with legacy C/C++ libraries
Low-level System Programming Device drivers, embedded systems, or OS-level operations
Memory-mapped Files Direct manipulation of large data structures

Important Considerations

  • Unsafe code requires the /unsafe compiler option to be enabled.

  • Memory leaks and access violations become possible with unsafe code.

  • The garbage collector cannot track pointers, so memory management becomes manual.

  • Use unsafe code sparingly and only when absolutely necessary.

Conclusion

Unmanaged code in C# provides direct memory access through pointers but sacrifices the safety and automatic memory management of the CLR. Use the unsafe keyword when performance-critical operations or interoperability with native code is required, but always prioritize managed code for general application development.

Updated on: 2026-03-17T07:04:35+05:30

737 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements