6

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".

Sellorio
  • 1,806
  • 1
  • 16
  • 32
  • 7
    Which platform has unmanaged C#? I've never heard of it. C# was designed for the .net managed environment, and remains so except perhaps if you're looking at mono touch/droid. If there isn't such a platform then perhaps that's the reason there isn't much to find out about it. – Preet Sangha Sep 24 '13 at 02:05
  • 3
    What on Earth is "Unmanaged C#"? O_O – Ivan Aksamentov - Drop Sep 24 '13 at 02:06
  • I have heard of C# that does not fall under memory management (and probably other stuff too). It allows the use of C++ style pointers. An example can be found by decompiling some .Net core functionality (I think). – Sellorio Sep 24 '13 at 02:11
  • 1
    Unsafe mode seems to be it. Not sure about the impact. – Sellorio Sep 24 '13 at 02:12
  • Get into browser or mobile games? They use unity and there are no issues. IIRC unmanaged C# means you can use pointers but that doesn't mean there wont be garbage collection so you have 0 reasons to go for that. Its mostly used to built wrappers to c libs –  Sep 24 '13 at 02:22
  • Changing the tone of my investigation I came across this: http://stackoverflow.com/questions/16231610/c-sharp-and-net-garbage-collector-performance which definitely reduces my worry about GC performance. – Sellorio Sep 24 '13 at 03:23
  • For the record. A lot of good answers here and I wish I could tick at least 2 of them. – Sellorio Sep 24 '13 at 23:03

3 Answers3

3

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.

Olexander Ivanitskyi
  • 2,202
  • 17
  • 32
  • It is not true that unsafe C# exist only for integration. Pointers and pointer arithmetic exist in C# for the same reasons they exist in C++. Unfortunately, pointers aren't "safe", i.e. the compiler and runtime cannot protect you from errors such as dangling pointers, buffer overruns and memory leaks, so the compiler requires you to agree to the disclaimer: "I know this is unsafe but I want it". – Kris Vandermotten Sep 24 '13 at 12:20
  • Well, C# was not primarily developed to work with pointers. – Olexander Ivanitskyi Sep 24 '13 at 12:22
  • I mean you can use pointers and handle memory control, but it is not what C# is designed for. – Olexander Ivanitskyi Sep 24 '13 at 12:29
  • 4
    Says who? Pointers have been available in C# from day 1. Sure, not every project requires them, but the same is true for operator overloading, for example. Would you say C# was not primarily developed for operator overloading? Maybe it wasn't developed primarily for abstract classes? Or even for structs? Maybe generics? Those weren't around in C# 1.0. What was C# developed _primarily_ for anyway? – Kris Vandermotten Sep 24 '13 at 12:30
  • 1
    I would say C# was primarily developed for .NET Framework. And .NET is a technology with Automatic Memory Management. I think you have answered this question in your first comment - pointers are not "safe", and unsafe code **is not allowed by default**. – Olexander Ivanitskyi Sep 24 '13 at 12:39
  • The fact that it is not enabled by default is not relevant for the debate. You claimed it exists only for integration purposes, and that it somehow is a less important feature. I claim it is part of the language since day one, designed, developed and documented by the C# team just as much as any other feature of the language. I use unsafe C# quite a lot in pure C# programs without any integration with native code. In fact, because I have unsafe C# I often don't need to go to native code. Your claim is that unsafe C# exists because of native code. I ask you: who says that is indeed the case? – Kris Vandermotten Sep 24 '13 at 12:46
  • Ok, @KrisVandermotten, you have a kind of persuaded me:), so I made the answer less categorical. But if you ask me "who says", check the comment of Eric Lippert on this question: http://stackoverflow.com/questions/2760924/safe-vs-unsafe-code. Thanks for comprehensive discussion!:) – Olexander Ivanitskyi Sep 24 '13 at 13:49
  • 1
    Ticked as much for the answer as it was for the excellent and descriptive discussion in the comments. Thanks :) – Sellorio Sep 24 '13 at 23:04
1

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.

Dave
  • 4,282
  • 2
  • 19
  • 24
  • 4
    MS has stopped further development of XNA. For a new project I would not recommend it. – cdoubleplusgood Sep 24 '13 at 05:59
  • @cdoubleplusgood Right you are. It's not exactly a dead technology just yet, but I suppose it's better to recommend tools that have a foreseeable future, huh? :) – Dave Sep 24 '13 at 06:24
0

For general purpose applications C# (managed) is a very suitable language with great performance. Unless you have extremely high demands you can certainly use it for games. Have a look at Unity:

Unity

Nevertheless, most AAA game studios use C++ as their main programming language. That being said, if you want a career in such studios you are better off investing some effort in C++.

  • I know c++ relatively well but I much prefer the features in c# such as Attributes, explicit Abstract and Static classes, Properties (love them) and the extensive and simple framework (probably more stuff I forgot). I'm hoping for a way to let me use C# without the performance hit (or less). – Sellorio Sep 24 '13 at 02:25
  • Even though I never feel as much at home as with C++, I do think C# has great features. Have you ever tried Reactive Extensions in C#? The stuff is amazing! It enables really powerful event-driven, parallel programming. –  Sep 24 '13 at 02:29
  • I have to look it up. Thanks :) – Sellorio Sep 24 '13 at 03:08