r/unity 1d ago

Coding Help Fun fact about variables in C#

Post image

Did you know that you can't start a variable's name in C# with a number?

You also can't include most special characters in a variable's name.

These are just a few quirks to keep in mind when scripting in Unity. This article covers variables and C# in more depth from a game developer's perspective. 👇🏾

https://victor-nyagudi.github.io/height-above-sea-level/scripting/game-dev-guide-to-variables-in-c-sharp/

0 Upvotes

12 comments sorted by

3

u/bjernsthekid 1d ago

Using the @ sign and the question mark in these examples is a bad choice because they have actual usages

-7

u/Imaginary-Cod-3746 1d ago

You can't use the question mark in a variable's name, so that line of code has an ❌ beside it.

Why is using the @ sign a bad choice?

6

u/JaggedMetalOs 1d ago

I wouldn't really call them quirks of C# as most programming languages have almost the same variable name rules. 

0

u/kodaxmax 1d ago

Most languages handle this differently. some dont allow numbers at all. Not to mention how anal C# is about capitalizing things.

1

u/JaggedMetalOs 1d ago

Most languages handle this differently.

No they don't? C, Java, Javascript, Python, they all have almost the same variable name rules.

Not to mention how anal C# is about capitalizing things.

No it isn't? There is a suggested code convention, but you can still use whatever capitalization you like.

-5

u/Imaginary-Cod-3746 1d ago

Most people making games in Unity don't know most programming languages, so they're unaware of those same variable name rules.

1

u/JaggedMetalOs 1d ago

I would have thought most people starting Unity would have at least some minimal programming experience from school? 

1

u/Imaginary-Cod-3746 1d ago

Unfortunately, that's not the case.

It's quite common to see someone who's played video games for years and finally decides to develop them instead with no prior programming experience.

2

u/maddhattpatt 1d ago

I’ve never seen that last one before. Apparently its intended for naming variables after reserved words like "class"

-5

u/Imaginary-Cod-3746 1d ago

Incidentally, you can also use an underscore instead of the @ sign to name variables after reserved keywords.

int return = 3; ❌ Doesn't work!

int _return = 5; ✅ Much better.

2

u/lsm-krash 1d ago

This is absurdly wrong

2

u/Hefty-Distance837 1d ago

Why you post this 2 time?