Advertisement

did i make a mistake starting with C#?

Started by September 15, 2020 09:11 PM
18 comments, last by hplus0603 4 years, 3 months ago

hi all, discussion thread.

i'm sure there is no straight answer to “which language is better”, however i just found out that all of my favorite games were made using C++, while i've been learning C# for the past 3 months.

should i quit my C# studies and move on o C++? or are the differences small enough for me to “finish what i've started” with c# before re-considering starting with C++?

am i going to have to learn everything from scratch?

thanks :/

None

Why not both?

Much of what you learn with C# transfers over to C++.

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

Advertisement

@undefined time, devotion, concentration… it's better to focus on one thing that does the job, is it not?

None

@SassyPantsy I would say stick mainly with C# but dabble in C++ occasionally.

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

Why not pick up Unity, and put those C# skills to good use?

Personally, I would go with C++. It has the STL containers, which do automatic garbage collection like a C# program would. As long as you stay away from new and delete, you'll be fine – you really don't need those.

taby said:

Why not pick up Unity, and put those C# skills to good use?

Personally, I would go with C++. It has the STL containers, which do automatic garbage collection like a C# program would. As long as you stay away from new and delete, you'll be fine – you really don't need those.

A correction: The STL containers are not doing any automatic garbage collection. You can have some form of a GC if you use smart pointers and STL containers, but it's very far from what C# would do.

OP: It depends on what you want to do. If you are interested in engine development and building your game from scratch, then c++ is a better choice, but if you just want to make games, then use Unity which uses C# for its game logic.

shaken, not stirred

Advertisement

I beg to differ. The main point of the class destructor is to automatically collect garbage. The collection occurs when the container goes out of scope, not some arbitrary time later.

taby said:
It has the STL containers, which do automatic garbage collection like a C# program would.

Could be reworded slightly…

“which APPEAR TO do automatic garbage collection like a C# program would.”

But I don't think arguing semantics here does the OP any good.

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

taby said:

I beg to differ. The main point of the class destructor is to automatically collect garbage. The collection occurs when the container goes out of scope, not some arbitrary time later.

Garbage collection is automatic per definition, however, and can happen at any point. The purpose of the destructor isn't to perform GC, it's to allow the author of the class to handle manual memory deallocation (which isn't GC)

@sassypantsy I would just stick with C#. There's no reason your favourite games couldn't have been written in C#. It's also a somewhat more forgiving language. But as it was said elsewhere, many of the concepts carry over to other languages.

SuperVGA said:
Garbage collection is automatic per definition, however, and can happen at any point. The purpose of the destructor isn't to perform GC, it's to allow the author of the class to handle manual memory deallocation (which isn't GC)

Yeah, a destructor is not a GC at all, you can return the memory you acquired while the class was alive but it doesn't do any memory reordering, defragmentation or whatever what C# does in the background. In C# we have finalizers that are called when a class is going out of scope as well, even if this means that this can happen at any time and not when the programmer intended it does because of the GC managing objects in the background.

SassyPantsy said:
should i quit my C# studies and move on o C++

Definitely no! I can't add enougth exclamation marks to this sentence as I wish.

Yes, games not working in Unity or a custom engine are made with C++ while even most custom engines are C++. It's simply because C++ has some advantages that C# doesn't has and vice versa.

  • C++ is a raw language which means that most of the time you have full control over anything you do, you allocate an integer on the stack and pass it's pointer (memory address) to a function that accepts a byte array because unless C#, everything in C++ is just memory. C++ also has 2 very powerful tools: Macros/Preprocessor and Templates which were the base concept of what C# calls Generics (which are way less powerful)
  • C# is a high level language that is mostly safe to use due to some decisions Microsoft made in the past like strong typing of your variables (you can't cast an int to a byte array), automatic memory management (the so often mentioned Garbadge Collector or GC) which observes everything you new and you don't have to care about where which object lives unlike in C++ where this can become a night full of try and error when your game crashes due to memory leaks. Also the amount of stuff that is already in the framework is a universe away from what you get in C++, you can for example make an HTTP request to a web service in less than 10 lines of code while in C++ you first have to invest a lot of work into getting the TCP connection handled while you don't even have started to parse HTTP Headers

And here lies the strengths (and weaknesses) of both languages, while C++ is very low-level kind of coding which doesn't care about type-safety (which is a plus in some situations), it lacks of the wide feature basis that comes with the .NET Framework. This makes C++ the ideal language to write code that relies on performance and interact right near the hardware. It's purpose is to be used in critical game systems but especially any aspect of a game engine.

C# on the other hand has a wide base of utility classes and you don't have to care about memory management that much as in C++ while it lacks performance in some aspects and is not that low-level. Some special cases you could solve in C++ with a few lines of code may also require significant more work in C# due to it's type-safe model but C# is true multiplatform compatible with .NET and Mono e.g. .NET 5 which aims to target Linux/Mac and Mobile as well.

TL;DR and this is what is widely used in professional game development: Use C++ when you need performance or write a low-level system like a game engine; use C# whenever you have the need for a tool, an editor or even as a script language for your C++ engine because you don't have to care about memory management that much.

So the right answer is to learn both ?

This topic is closed to new replies.

Advertisement