9. Data types#

As you dive deeper into the world of coding, you’ll find that the values you’ve learned to store in variables can be quite different from one another. Just as jars can contain marbles, an apple, water or sand, variables in C# can hold various types of data. Each of these types has unique properties, and each has its own name. In programming, we call these different types of data ‘data types’.

In your first program, you’ve already encountered a data type: the string. When you wrote Console.WriteLine("Hello, World!") you used a data type called string that contains a sequence of characters. The text "Hello, World!" is a string.

C# has many built-in data types and eventually we will learn to build our own. Here are a few key built-in types.

  • Integers (int) are used to store whole numbers, without a decimal point. For example, the number of marbles in a jar would be stored as an integer.

  • Booleans (bool) represent truth values, which can be either true or false. Imagine you have a label on a jar that indicates whether it’s full or not - this could be a boolean.

  • Characters (char) are used to store a single character, like 'a', 'b', 'c', etc. Each letter on a label of a jar could be considered a character. You declare a variable with a specific data type like this:

  • Strings (string) are used for storing sequences of characters, like words or sentences. The label on a jar could be considered a string.

Understanding data types in programming is like understanding different categories of things in real life. For instance, there are different categories for vehicles like cars, bikes, trucks, each with their unique characteristics and purposes.


Fig. 9.1 If you want me to give you a bolt but I give you a nut then I’ve misunderstood the type of thing that you need. A data type captures the idea that things can be of different types.#

You declare variables with specific data types like this:

int numberOfMarbles;
bool isJarFull;
char firstLetterOfLabel;
string label;

And you can assign values to these variables like this:

numberOfMarbles = 5;
isJarFull = true;
firstLetterOfLabel = 'A';
string label;

When we’re declaring a variable to be of a particular data type, we’re telling the compiler that whatever value the variable contains it will be of that type. A data type defines a set of values that are members of that type, or a ‘value space’.

What happens if you try to store the wrong type of value in a variable? Let’s say you tried to put the number 5.5 (a non-whole number) in the numberOfMarbles variable. The C# compiler would throw an error because 5.5 isn’t an integer. It’s important to choose the correct data type for the kind of value you want to store.

numberOfMarbles = 5.5;
(1,19): error CS0266: Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?)

Similarly, we cannot store a string in a variable of type int or char.

firstLetterOfLabel = "Fruit";
(1,22): error CS0029: Cannot implicitly convert type 'string' to 'char'

We’ve mentioned the concept of null and how it’s a value that represents the absence of a value. For some types, like string, null is a valid value, and for some, like int, it is not. We will discuss this later but the difference between these two categories of types in C# is that the former is a ‘reference type’ and the latter a ‘value type’.

This means you can assign null to a variable of type string like this:

string name = null;

But you cannot assign null to a variable of type int:

int age = null;
(1,11): error CS0037: Cannot convert null to 'int' because it is a non-nullable value type

There are many more data types in C#, and as you progress, you’ll encounter and learn to use them. The data type you choose will depend on the kind of data you’re dealing with in your code. For now, understanding these basic data types will equip you with the tools to start writing more complex and interactive programs!

In the next chapter, we’ll learn about how we can use these different data types together to perform calculations and operations in our programs. This will introduce us to the concept of ‘expressions’.