Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unmanaged C# versus C++ [closed]

Tags:

c++

c#

unmanaged

I am a wanna-be Games Developer and I prefer using C#. When I asked what the disadvantages of writing real-time applications in C# were I got 1 significant point back: Garbage Collection and the unpredictable impact it can have on performance.

My counter question is, what about Unmanaged C#? How does it compare (performance-wise) to C++? Is it a valid option for developing software?

I don't hear much about unmanaged c# and all the "unmanaged c# versus C++" questions I saw were unanswered or answered inaccurately. These questions were not on stack overflow.

EDIT:

I believe umanaged C# is "Unsafe Code".

like image 797
Sellorio Avatar asked Sep 24 '13 02:09

Sellorio


2 Answers

Unsafe code in C# is not for developing separate applications. You can use unsafe code for some kind of time-critical operations, but generally it's not the most effective and the most convenient way of doing this. IMHO, it is primarily designed to give C# an opportunity to integrate with unmanaged dynamic link libraries and unmanaged code, so from my point of view the primary reason is INTEGRATION.

I can see 3 common ways of managed and unmanaged code integration:

  1. Unsafe code in C# and P/Invoke. Build C# wrappers over compiled unmanaged DLLs.
  2. Managed C++. Build managed assemblies over existing C/C++ code.
  3. COM interoperation. Call Runtime Callable Wrapper from .NET client or call COM Callable Wrapper from COM client.

On the other hand, it's your architectural and conceptual decision: if you need a full memory and performance control, you develop in C++ or even pure C. If you need advantages and simplicity of modern language and modern technologies, you develop in .NET C#. Or you can use both, and how to integrate them is described above.

like image 90
Olexander Ivanitskyi Avatar answered Oct 05 '22 14:10

Olexander Ivanitskyi


You can use C# to build games. The question is what exactly are you intending to do? What platforms do you intend to target, and how polished do you intend the finished product to be?

Others have mentioned Unity, which uses C# and provides a ready-made game engine and development suite. The only downside is that the free version has limitations.

If you want to build your own engine for the sake of understanding, look into XNA. Or you can use a wrapper around OpenGL like SharpGL. Or maybe you can find the long-dead Managed DirectX floating around somewhere. Or if you are really brave, you can use unsafe code and wrap GDI calls so that you don't have to deal with the horribly slow GDI+ implementation. The last two really aren't recommended, and only XNA is going to provide you more than a way to draw things on the screen. There are sure to be countless other possibilities, especially considering what becomes available to C# developers with Mono.

Whatever you decide, the garbage collector isn't going to get in your way, and unsafe code wouldn't be a solution if it did.

Edit: As mentioned by cdoubleplusgood, XNA is no longer in active development. Look into Monogame and consider the wonders of cross-platform development a bonus.

like image 38
Dave Avatar answered Oct 05 '22 12:10

Dave