
Declaring variables
In the previous chapter, we saw how variables are written and touched on the high-level functionality that they provide. However, we're still missing the syntax that makes all of that possible. Variables don't just appear at the top of a C# script; they have to be declared according to certain rules and requirements. At its most basic level, a variable statement needs to satisfy the following requirements:
- The type of data the variable will store needs to be specified.
- The variable has to have a unique name.
- If there is an assigned value, it must match the specified type.
- The variable declaration needs to end with a semicolon.
The result of adhering to these rules is the following syntax:
dataType uniqueName = value;
Variables need unique names to avoid conflicts with words that have already been taken by C#, which are called keywords. You can find the full list of protected keywords at https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/index.
This is simple, neat, and efficient. However, a programming language wouldn't be useful in the long run if there was only one way of creating something as pervasive as variables. Complex applications and games have different use cases and scenarios, all of which have unique C# syntax.
Type and value declarations
The most common scenario for creating variables is one that has all of the required information available when the declaration is made. For instance, if we knew a player's age, storing it would be as easy as doing the following:
int currentAge = 32;
Here, all of the basic requirements have been met:
- A data type is specified, which is int (short for integer).
- A unique name is used, which is currentAge.
- 32 is an integer, which matches the specified data type.
- The statement ends with a semicolon.
However, there will be scenarios where you'll want to declare a variable without knowing its value right away. We'll talk about this topic in the following section.
Type-only declarations
Consider another scenario: you know the type of data you want a variable to store, as well as its name, but not its value. The value will be computed and assigned somewhere else, but you still need to declare the variable at the top of the script.
This situation is perfect for a type-only declaration:
int currentAge;
Only the type (int) and unique name (currentAge) are defined, but the statement is still valid because we've followed the rules. With no assigned value, default values will be assigned according to the variable's type. In this case, currentAge will be set to 0, which matches the int type. Whenever the actual value is available, it can easily be set in a separate statement by referencing the variable name and assigning it a value:
currentAge = 32;
You can find a complete list of all C# types and their default values at https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/default-values.
At this point, you might be asking why, so far, our variables haven't included the public keyword, called an access modifier, which we saw in earlier scripting examples. The answer is that we didn't have the necessary foundation to talk about them with any clarity. Now that we have that foundation, it's time to revisit them in detail.