"Using" vs [DllImport]?

Kourosh picture Kourosh · Mar 11, 2011 · Viewed 7.7k times · Source

I was wondering what is the very top most declared references and why we still need to use DllImport? I'm talking C#.

Answer

wsanville picture wsanville · Mar 11, 2011

From the MDSN documentation:

The DllImport attribute is very useful when reusing existing unmanaged code in a managed application. For instance, your managed application might need to make calls to the unmanaged WIN32 API.

Basically, when you're writing a .NET application, and a library does not have a managed wrapper (it's written in unmanaged code), you need to use DllImport to interoperate with it. Otherwise, you can reference managed libraries with a using statement like you normally would any base class library.