Variables – C# Introduction – Basic Concepts

Variables – C# Introduction – Basic Concepts

In C# Unity, a variable is a named storage location used to store a value of a particular data type.

Variables are an essential part of programming languages, as they enable us to store and manipulate data at runtime.

In C# Unity, there are three main types of variables:

  1. Value types: These are variables that store values directly.

    Examples include int, float and bool.
  2. Reference type: These are variables that store references to values that stored elsewhere in memory.

    Examples include string, Array and Object.
  3. Pointer types: These are variables that store memory addresses.

    Pointer types are rarely used in C# Unity.

Here are a few examples of using variables in C# Unity.

// Declare a variable of type int
int intNum;
intNum = 6;

// Declare and initialize a variable of type float
float floatNum = 6.66f;

// Declare and initialize a variable of type string
string strName = "Hey Yo";

// Declare and initialize an array of ints
int[] intArray = {1, 2, 3, 4, 5, 6};