Variables and Data Types

Explore C#'s syntax by creating variables and learning about data types.

Variables

No app is useful if it can’t store and process information. In this context, variables are important.

Variables are used to store data within a program. They represent a named area of computer memory that holds a value of a particular type. The type determines what kind of information a variable can store.

The basic syntax for declaring a variable is as follows:

variable_type variable_name;

Valid variable names must meet the following requirements:

  • The name can contain an underscore and any number and letter, while the first character in the name must be a letter or underscore.
  • The name can’t contain punctuation marks or spaces.
  • We can’t use a C# keyword as a variable name.

Although we can give any name to our C# variables, it’s highly recommended to use descriptive names. For instance, if we want to store a person’s first name, we can declare a variable as follows:

string name;

In the example above, we create a variable called name that has the string type. The string objects represent text.

Remember that C# is case-sensitive, so the following two variable definitions represent two different variables:

string name;
string Name;

We’ve declared a variable, but it doesn’t contain any information yet. To assign a value to a variable, we use the assignment (=) operator.

name = "John";

We can combine declaration and assignment in one line. This operation is called initialization.

string lastName = "Doe";

We get an error during compilation if we try to assign a value to a variable that wasn’t declared:

C#
using System;
namespace Variables
{
class Program
{
static void Main()
{
age = 24;
Console.WriteLine(age);
}
}
}

We also get an error if we try to assign a value of the wrong type:

string notANumber;
notANumber = 20; // error
int aNumber = 20; // correct

Use the exercise below to practice variable creation. Initialize two variables needed for correct compilation and meaningful output.

C#
using System;
namespace Variables
{
class Program
{
static void Main()
{
// Create suitable variables below to get meaningful output
Console.WriteLine($"Hello, my name is {name} and I am {age} years old");
}
}
}

A variable can have a different value at different stages of the program. That is, we can reassign a variable to have another value.

int length = 29; // Create a variable
length = 56; // Assigned a different value

Types

Like many other programming languages, C# has its own data type system. The data type defines the internal representation of the data, the set of values that an object can take, and the set of actions that can be performed on the object.

Primitive types

Primitive types are the basic building blocks of C#. The following table lists the most commonly used types:

Data Type Description
bool Can either be true or false
int A whole number (4, 42, 70) represented by four bytes of memory.
byte This is also a whole number, but because it’s represented by only one byte of memory, it can only hold values from zero to 255. Unlike int, it can’t hold negative values.
long This is a whole number represented by eight bytes of memory. To create a literal of the long type, add an appropriate suffix at the end of the number: 30L.
float A single-precision floating-point number that can have values ranging from -3.4 ×\times 1038 to 3.4 ×\times 1038. This uses four bytes of memory. Literal: 17.5f.
double This is a double-precision floating-point number that uses eight bytes of memory. All real number literals (23.27, 78.9) are of the double type by default.
decimal A 16 byte decimal number. Literal: 1009.621m.
char This represents a single Unicode character and uses two bytes of memory. A char literal comes between (single quotes) '' ('l', '2', 'g')
string This stores a series of Unicode characters. The amount of memory used depends on the length of the string. String literals are enclosed in double quotes ("I am a string literal.")
object This is the base type in .NET. All types are derived from this object type. On 32-bit operating systems, it weighs four bytes, while on 64-bit systems, it’s eight bytes.

Arrays

An array is like a container that holds a fixed number of elements of some type. An array declaration is similar to a variable declaration, except that we put square brackets ([]) after the type:

string[] students;

After defining an array variable, we can create an array object and assign it to the variable:

string[] students = new string[10];

In the code above, we declare a students array that holds values of the string type. The new keyword allocates the amount of memory required for this array and fills the array with the type’s default value.

Visual representation of an array
Visual representation of an array

In this case, we declare that we need enough memory to hold 10 string objects and initialize the array with default values for the string type. The default value for string is null, which signifies the absence of a value.

Some other ways to initialize an array are as follows:

C#
using System;
namespace Arrays
{
class Program
{
static void Main(string[] args)
{
string[] students1 = new string[3] { "Aidil", "John", "Patrick" };
string[] students2 = new string[] { "Aidil", "John", "Patrick" };
string[] students3 = new[] { "Aidil", "John", "Patrick" };
string[] students4 = { "Aidil", "John", "Patrick" };
Console.WriteLine($"Array 1 length is {students1.Length}");
Console.WriteLine($"Array 2 length is {students2.Length}");
Console.WriteLine($"Array 3 length is {students3.Length}");
Console.WriteLine($"Array 4 length is {students4.Length}");
}
}
}

Run the code above to confirm that, in all cases, an array with a length of 3 is created.

To refer to an element inside an array, we use the element’s index, which is its position inside the array. Keep in mind that numeration starts at index 0. In other words, to get the first element of an array, we do the following:

Console.WriteLine(students1[0]);

If we have an array of length 3, like in the previous example, we can’t access an element with index 5, for instance. To check what happens if we try to go out of an array’s boundaries, run the following code.

C#
using System;
namespace Arrays
{
class Program
{
static void Main(string[] args)
{
string[] students = new string[3] { "Aidil", "John", "Patrick" };
Console.WriteLine(students[3]);
}
}
}

An error occurs because we try to access the fourth element (index 3), but the array only holds three items.

Common Type System

Note that the type system is the same for all languages of the .NET family because it’s common across the languages. The so-called Common Type System (CTS) enables language interoperability in .NET. When we create a System.String object in C#, it’s essentially the same System.String object in Visual Basic and F#, although different aliases could exist in different languages.

For example, string in C# is an alias for the System.String type, while int is an alias for System.Int32. Therefore, when we do something like this:

int myAge = 25;
System.Int32 yourAge = 30;

We essentially create two variables of the same type. Run the code below to confirm.

C#
using System;
namespace DataTypes
{
class Program
{
static void Main(string[] args)
{
System.Int32 firstNumber = 56;
int secondNumber = 54;
Console.WriteLine($"firstNumber is of type {firstNumber.GetType()}");
Console.WriteLine($"secondNumber is of type {secondNumber.GetType()}");
}
}
}

In Visual Basic, Integer serves as an alias for System.Int32, while strings can be created using the String alias.