What is difference between unsafe code and unmanaged code in C#?
Unsafe code runs inside the CLR while un-managed code runs outside the CLR.
An example of unsafe code would be:
unsafe class MyClass
{
private int * intPtr;
}
You can use pointers anywhere in this class.
An example of unmanaged code is:
class MyClass
{
[DllImport("someUnmanagedDll.dll")]
static extern int UnManagedCodeMethod(string msg, string title);
public static void Main()
{
UnManagedCodeMethod("calling unmanaged code", "hi");
}
}
It is not necessarily unmanaged code itself, but calling it.