C#: Getting to know variables

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.

Creating a variable in C#

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.

Variable data types

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Trying to Know and configure flutter in first glance .

Libre Software isn’t Radical (RFC)

[New Coin Listing] Raydium(RAY) to Be Listed on DigiFinex

AWS SSO Integration with Common Business Applications

Flutter Thursday 13: Building a User Registration and Login Process with provider and external API

Setup Java-How to Program in Java

How to Handle States in Jetpack Compose

Why Object Oriented Matters

Go to the profile of The Sharp Ninja

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ian Plumpton

Ian Plumpton

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

More from Medium

What is the Middleware

Generic Method and Generic Class in C#

Unit Testing in C#

Pangram in C#