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 ?