JBobby wrote:
Am new to C#. Please tell me in two lines what are the disadvantages of using unmanaged code?
Sure.
First of all, compared to .NET, unmanaged code is very, very error-prone. Abhinav mentioned just one problem in his comment to Solution 2: memory deallocation. But this is not the biggest problem is you are using unmanaged module in your .NET project.
The biggest problem is this: in most cases, it
kills platform compatibility. .NET has one wonderful feature: you can execute your CLI code on different systems, even on different CPU
instruction-set architectures,
without recompilation. This is possible due to "AnyCPU" target and JIT compilation, which is performed during runtime. Moreover, there are alternative CLR implementations which allows you to do the same on many non-Microsoft OS (most used implementation is called Mono). When you use P/Invoke to use unmanaged code, you, in almost all cases, kill this possibility.
Please see:
http://en.wikipedia.org/wiki/Just-in-time_compilation[
^],
http://en.wikipedia.org/wiki/Common_Language_Infrastructure[
^],
http://en.wikipedia.org/wiki/Common_Language_Runtime[
^],
http://en.wikipedia.org/wiki/Mono_software[
^],
http://www.mono-project.com/Main_Page[
^].
I'll strongly recommend to use only the pure .NET FCL (or even just BCL), possibly with pure-CLI third-party products, especially open-source. Use unmanaged (native) modules only if they are critically important and if there is absolutely no other way (with P/Invoke or C++/CLI).
See also:
http://en.wikipedia.org/wiki/Framework_Class_Library[
^],
http://en.wikipedia.org/wiki/Base_Class_Library[
^],
http://en.wikipedia.org/wiki/P/Invoke[
^],
http://msdn.microsoft.com/library/en-us/vcmxspec/html/vcmg_PlatformInvocationServices.asp[
^],
http://en.wikipedia.org/wiki/C%2B%2B/CLI[
^],
http://www.ecma-international.org/publications/standards/Ecma-372.htm[
^],
http://www.gotw.ca/publications/C++CLIRationale.pdf[
^].
[EDIT]
After changes in Microsoft documentation, most adequate link to P/Invoke and C++/CLI
implicit P/Invoke is this:
https://msdn.microsoft.com/en-us/library/ms235282.aspx[
^].
—SA