Skip to main content
GameDev.net gamedev.net
🔒 Locked

did i make a mistake starting with C#?

Started by SassyPantsy Sep 15, 2020 at 9:11 PM 18 replies 13.9k views
Original Post
SassyPantsy
SassyPantsy

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
fleabay
fleabay

Why not both?

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

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

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

None
fleabay
fleabay

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

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

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.

sirpalee
sirpalee

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
taby
taby

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.

fleabay
fleabay

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

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.

Shaarigan
Shaarigan

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 ?

SassyPantsy
SassyPantsy

@undefined thank you so much for this answer!! This has put a lot of sensein my head right now, and im gonna do just that. I'll continue with c# while also going through some c++ learning. I want to have a better understanding of memory management and the lower lecels you mentioned m thanks!!

None
SassyPantsy
SassyPantsy

@Shaarigan amazing answer dude, thank you for your time. i'll defiantly work by that, start making unity games with C# while also learning C++ for more complex projects down the line. thanks again for this explanation!

None
8Observer8
8Observer8

SassyPantsy said:
start making unity games with C# while also learning C++ for more complex projects down the line.

Why do you not study Unreal Engine? Concentration on one thing is a good idea. You can spend all time on C++ (not half of time). I think if you study C++ you will want to start to make games using C++. You will spend time on Unreal. Start to study Unreal Engine and C++ and forget about C# and Unity.

Juliean
Juliean

While I do agree mostly with C# being easier than C++ due simpler memory-managment, I also want to make a point at how sometimes C# is so stupidly more complicated to use than C++ that it actually hurts for it to be called a “high level” language. A few examples:

//C++ 
Vector2 v;
Vector2 v2(10, 10);

//C#
var v = new Vector2();
var v2 = new Vector2(10, 10);

Like, wouldn't you expect the “high level” language to have the version the uses less code? And I'm being even generous with using “var” which some people might not even do. Or how about this:

// C# Properties - awesome, sometimes (Unity-example)
meshRenderer.material.color.a = 10; // nope, that won't work.

// thats how you got to do it:
var modifiedColor = meshRenderer.material.color;
modifiedColor.a = 10;
meshRenderer.material.color = modifiedColor;

Now, again please don't get me wrong, C# has some really awesome things that I wish C++ had, but at the end of the day I'm always kind of baffled when I go from the so-called “low level” language writing really compact, easy to understand code and then I get to work and have to do stupid shit like what I posted.

1024
1024

SassyPantsy said:
however i just found out that all of my favorite games were made using C++

This doesn't matter. Any game that a C++ programmer can make using C++, a C# programmer can make using C#. So, if that was the only reason why you worry about your choice, you can stop worrying ?

a_insomniac
a_insomniac

To be honest with you. If you are just starting out learning how to code the language doesn't matter. Simply because you don't know what you don't know.

That said, you're not even close to being in a position yet to appreciate the benefits of one language versus another or even know why you need a particular feature or nuance that would make a difference in your work flow. Sure you may have an idea, but experience teaches you better.

Therefore, if you've already started down the path of C# then stick it out. As your overall goal initially should be that of becoming a “Good Programmer”. With the right resources C# will give you exposure to everything you need initially to be a “Good Programmer”.

Focus on learning how to write good code. Look up the S.O.L.I.D Principles and study them, keep them in mind while you're learning. Hone your understanding of everything OOP. Truly understand those concepts while applying all of it's parts in your journey. Once you have a good understanding of the language start solving small problems with the language, e,g., while working on your game build utilities for your game, build small utilities for yourself or others.

Once you're comfortable with the language. Continue with C# and start to focus on Data Structures and Algorithms. Again, the goal is to become a “Good Programmer”. Although I'd argue that you're still years off from that. It is an ocean of stuff to lean and the languages are nothing more than oars you paddle with in comparison. If you're not already a member, I would suggest joining something like Codewars and do a couple of those everyday. Nice and focused problems that you need to solve allowing you to hone your skills with whatever language you're using.

Reaching this point with C# you should be able to easily create some pretty useful programs that solve a need to a certain degree. You should also feel comfortable in any space where C# is the main language, i.e., Unity, GoDot, etc. As you're basically left with learning the API's and understanding the architectural structure of the environment you're attempting to work in.

More importantly, at this stage. You'll also come to realize that asking a question like “Should I start with X language vs another” doesn't matter. As you now have the skill set to easily pick up whatever language when needed in a matter of days (requirement of professional programmers), not weeks or months because you've already put in that work with C#. It is at this stage on the road to becoming a “Good Programmer” that as you start to pick up languages like C, C++, JavaScript, Rust, Python, Go, etc. That you discover why they exist, what frameworks rely on them, and why all of that is useful. Again, languages are nothing but oars in a ocean of technology that takes a while to traverse.

So stick with C# for now. It doesn't have to be your main or your favorite language. It's just the first stone laid in the path on your way. Walk it and keep growing. Trust me, you'll know when you're ready to move on and when you should.

As for me, I've been coding for about twenty years. I've been where you are so I get it. I'm a software engineer for a large tech firm. Main language is C#/JavaScript and we use the Angular Framework in certain parts of the software. I've coded in so many languages over my career it's insane. From COBOL to Perl, from Vb 6 to C++.

I'm telling you all of this because you'll eventually come to realize that when it's all said and done the language doesn't matter. Knowing how to code, how to solve problems, how to design solutions, how to adapt and quickly pick up new technology, how to keep up with and understand the ever changing landscape of software development is the real goal.

Take care and good luck.

----------
 
SassyPantsy
SassyPantsy

Ay ay captain. Thanks a lot for this reply. I understand and agree, and ive sat down in front of my goals and realized that i want to make and publish a title in a year, and therefore the language is only (as you said) and oar to paddle with for this moment.

None
enigma_dev
enigma_dev

FWIW I attempted to learn c++ on my own, but didn't get very far because there's a whole lot of features in c++ and it can be really easy for a new learner to shoot themselves in the foot (eg memory stomping, it can be really easy to accidently overwrite something else's memory with an out of bounds array access – and debugging those can be a pain).

I ended up learning java (like C#) in university. After learning java, I revisited c++ and things made a lot more sense. So I studied C++ after java (while still in university) and now I have a job using c++.

I feel like learning a higher level compiled language before c++ actually helped me with learning c++. I got used to having garbage collection with java/c# then had to learn to use RAII (ctor/dtor) for c++. It was good to contrast the two IMO.

Plus I think a lot of game companies may write tools in c#.

While UE4's tools are probably c++, I know UE4's module system is actually C# files, not cpp. But the game code is written in c++/blueprint(visual scripting). However even in the UE4 c++ code there are “events” and “delegates” which I *think are based on C# concepts.

So, I'd stick with learning C# first. then visit cpp after you've learned it and made a small scoped project or two using C#. After that learn to do everything you know how to do in c# in cpp. That will strengthen your knowledge in both languages.

hplus0603
hplus0603

Every game that shipped in Unity in the past few years, is a C# game.

Every game that shipped on the Xbox 360 with XNA Live Arcade, was a C# game.

It's totally possible to ship fun games in C#.

The path to learning game programming can be broken into these four steps:

  1. Learn to program.
  2. Learn to program well.
  3. Learn to program the machine well.
  4. Learn to think well about programming.

You're at step 1. C# is fine. For step 2, C# is also fine. For step 3, certain parts of the machine can be pushed by C# just fine – configuring textures/shaders, for example. Other parts, will start benefiting from the lower level control of C++ or Rust. For step 4, you will find that higher-level approaches, like Haskell, have more to offer. Once you're past step 4, you will be able to pick the right tool for each job! The journey there is long and exciting, and exactly which path you take, doesn't matter as much, as keeping walking the path!

enum Bool { True, False, FileNotFound };

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.