C#: Getting to know variables

What are variables and how are they used?

One of the key elements of coding is the variable. A variable is like a container that allows code to store information. It can then be used for calculation, manipulated and passed to other pieces of code.

In gaming terms a variable might contain the player’s health, score, name or any number of different values. Let’s examine them below.

Pseudo-structure of a variable in C#

Above is the structure of a variable in C#. Three of the elements are required, one is optional.

  • Access modifier: this determines the visibility of the variable to other scripts or classes within a program. Generally this will be either private (only accessible to the rest of the class/script) or public (accessible by other classes/scripts).
  • Variable type: this determines the type of information the variable will contain. Common types include int, float, string or bool. I will explain these types below.
  • Variable name: this gives the variable a name so it can be called within the rest of the code. In general a variable can be called anything, with a few prohibited exceptions. Good practice is to name a variable that makes it easy to understand, i.e. playerHealth.
  • Initial value: the fourth parameter is the optional one and that is to assign a value to the variable when it is created. In C# you can create empty variables and assign values via code later.
Example variable

Putting that into an example, we can create a private integer (int) called _playerHealth and we give it an initial value of 100. Note the underscore before the variable name; you don’t have to do this but many developers use this to identify private variables in a class. When you’re 1000 lines deep in a script, it saves you from having to scroll to the top to check that variable’s access type.

  • int: this is short for integer and variables of this type hold whole numbers, i.e. 100.
  • float: this variable type contains fractional numbers or numbers with a decimal place, i.e. 2.5. When assigning values to float variables, the number must always be followed by the letter ‘f’, i.e. 2.5f
  • string: this variable holds a string of text. These can contain numbers as well but they won’t be able to be used for calculations like ints or floats without some additional code first.
  • bool: this variable type holds either a true or false value. These are useful for storing state within code.
Examples of variable types

Above are examples of these four common variable types and how they might be declared. You can declare variables without assigning a value.

Working with variables

Variables use the standard operators (+, -, /, *, =) where possible. You can’t really do multiplication with a string, for instance, but you can with ints and floats.

As in the above example, the assignment operator is used to assign a value and you can do this in code to vary a value at runtime, hence the name ‘variable’.

Software developer in the field of Unity and C#. Passionate about creating and always driven to develop.