Grokking C# as a Python Programmer, Part 1


Much of the game development for a Unity-based game happens in C#. There are exceptions to that – shader programs are still written GLSL, for instance, and there’s some crusty Javascript API stuff floating StackOverflow, to be avoided – but by and large the bread and butter of coding a game in Unity is an amalgamation of C# scripts. I would argue that overall Unity game development is really a hybrid blend of visual programming, artistic or creative tooling, and more conventional “development”, but that’s beside the point here. For now, I want to review some high level observations about C# having come from a Python background.

Both Python and C# are extremely popular languages among professional developers, though they occupy different niches. Python developers are likely to be found in scientific computing, machine learning, data science, web development, and more and more frequently in IoT or embedded applications (check out the Circuit Python and MicroPython projects). C# cut its teeth on native Windows application development, and is now found in game development, web development, cross platform GUI programming, and elsewhere. According to the 2021 Stack Overflow developer survey, Python was the 4th most popular language among professional developers, and C# was the 8th most popular.

Origins

It becomes almost immediately apparent when you first step into C# that this is Microsoft’s town you are visiting. All the official documentation, tutorials, and other materials are hosted by Microsoft, and in fact the language itself was proprietary since it’s creation in 2000 prior to being open sourced in 2014. Coming into the language in 2022, there’s no perceptible constraints on the presence of the language and it’s perfectly simple to get up and running in C# (really, the underlying runtime and ecosystem of APIs known as dotnet) on any platform from Linux to Mac to Windows.

Conversely, Python has been open sourced since it’s very beginning, also in the year 2000. That may be part of the meteoric rise of Python across varied domains like scientific computing, data science, and web development, but it’s only part of the story. Until 2019, decision-making about Python was controlled solely by it’s creator, the “benevolent dictator for life” Guido Van Rossum, who stepped down at that time and was replaced by a five-member steering council.

All that to say, “open source” alone doesn’t tell one much about the governance of a programming language and it’s perfectly likely to find a popular and well-received language like C# come from a proprietary background.

“Scripting” Languages

A popular (mis)conception of both Python and C# is that they are primarily “scripting” languages above all else. On the contrary, as we’ve seen both are robust and fully capable of deployment in a vast array of contexts from machine learning to embedded systems. I think the implication that both are meant for scripting (and by insinuation, perhaps less serious that other languages) stems from the fact that both Python and C# are popular entrypoints to the world of programming overall due to their use in education and in game development, and, well, for writing effective scripts actually. In the case of Python, one will commonly see Python scripts used for exploratory data analysis by scientists, or as an (arguably more productive) stand in for Bash scripts by sysadmins, SREs, and others as the “glue code” for all sorts of operations tasks. C# is used in the Unity game engine as the scripting language to enable game designers to add custom logic to pretty much every conceivable object or aspect of their game. Seriously, C# scripts are quite literally “attached” to game objects via a drag and drop process in the Unity Editor! As you might expect, in order to support game development, the Unity C# ecosytem adds a whole additional layer of APIs and framework-specific behaviors on top of the standard dotnet environment.

What about the languages themselves?

There’s some commonalities and differences that are easy enough to point out between Python and C# – both are object-oriented; Python is dynamically typed, C# is statically typed; Python is interpreted, and C# is compiled; Python features significant white space while C# is a C-like language full of curly braces and semi-colons; so on and so forth. Next time, I’d like to talk a little about the “Python way” of doing things versus the “C# way” of doing things, particularly as it relates to the logical structure of programs and what that means for managing state across applications.

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *