Skip to content

Type

In order to build programs, we need ways of making sense of data within the computer. Looking back to the chapter on Digital Realities, remember that computers are electronic devices and that everything else on top of that is an abstraction that we build. So, how do the tools we have allow us to use different kinds of data within our programs?

The answer is that programming languages provide us with a mechanism to assign a type to different pieces of data. All values within a program have a type. The type indicates how the data stored in the computer’s memory should be interpreted by the program, and how it can be used. For example, you can use mathematical operations on numbers, but not text.

There are three basic data types available in a programming language:

  • Textual data such as "Fred", "Hello World", "23" and "This is text!".
  • Whole numbers such as 1, 0, -5, and 37.
  • Real numbers such as 0.5, -126.0, 3.141516 and 23.981.

The concept map below shows the concepts related to types.

A type defines how a piece of data in a program is interpreted, and what operations can be performed on the data.

Types — when, why, and how

As you work with code you will need to be able to classify the different pieces of data within your program using types. This will allow you to determine where you can use the data, and what actions you can perform on it.

Data types are just abstractions that we use to help us create digital realities. Most of the time you can ignore lower level details, but it can be good to remember that data is really just a binary value, which is itself an abstraction of the current flowing through the circuits within the computer.

Whichever type you are working with, you need to make sure that it will be suitable to capture the information you need. With whole numbers, check the range of values that the type can support. Different types of real numbers have different level of precision, which results in a trade-off between the range of numbers they can represent and level of precision they can capture — you will need to consider this. With text, you usually use the one data type unless you have a specific requirement for a single character.

In C#

Types are used to define how data is interpreted and the operations that can be performed on it. A data type will have a size in memory and, in the case of numbers, a range of valid values.

Text Types

There are two main text types in C#. You will use string most often, as it describes a sequence of zero or more alphanumeric characters. char, on the other hand, describes a single alphanumeric character. As we learned in literals, a literal string value is enclosed in double quotes ("), and a character value is enclosed in single quotes ('). This difference in representation helps us to quickly identify what kind of text data a literal value is.

NameSize
char2 bytes/16 bits
stringvariousa

a The size in memory is determined by the number of characters within the string, plus some overhead.

Whole Number Types

With whole numbers, you have signed and unsigned versions of the types. Signed numbers can be positive or negative, whereas unsigned numbers can only be positive.

You can see the main options for whole number data types in C# in the table below. Typically you will use int unless you have a specific reason not to.

Name
SizeRange (lowest .. highest)
byte1 bytes/8 bits0 .. 255
short2 bytes/16 bits-32,767 .. 32,767
int4 bytes/32 bits-2,147,483,648 .. 2,147,483,647
long8 bytes/64 bits-9,223,372,036,854,775,807 .. 9,223,372,036,854,775,807
uint4 bytes/32 bits0 to 4,294,967,295
ulong8 bytes/64 bits0 to 18,446,744,073,709,551,615

Real Number Types

With real numbers, the precision of a type is defined as the number of significant digits it can support. This is the number of digits the type can support, excluding leading zeros and zeros after the decimal point but before the first non-zero digit (e.g., the number 0.00298008 has 6 significant digits).

One factor to consider when you are using real numbers is that the way they are stored in memory means that you can only represent close approximations of any odd value. An important consequence of this is that rounding errors can accumulate over time.

NameSizeRange (lowest .. highest)Significant Digits
float4 bytes/32 bits±1.5 x 10−45 to ±3.4 x 1038~6 to 9
double8 bytes/64 bits±5.0 × 10-324 to ±1.7 × 10308~15 to 17

Types in Expressions

You can use mathematical operators to work with values in your code. The following table shows the operators that can be used on each type of data within an expression.

Type of dataOperations Permitted
Whole Numbers( ) + - / * %
Real Numbers( ) + - / *
Text( ) +

Type Conversions

You will occasionally need to convert data between different data types. A common example of this is how we will handle user input. When the user enters data at the terminal it will come into the program as text. So if the user enters 3 and then 2, it will come into the program as “32” - a string. This makes sense as the user can type any characters at the terminal. But what if you want that as an integer?

To help achieve this programming languages provide utility methods that will allow you to convert between different types. In C#, System.Convert provides many methods you can use (see details on the .net site). The main ones that we will use are:

  • ToInt32 - this can convert a string to an int.
  • ToDouble - this can convert a string to a double.

For example, ToInt32("73") will give you the integer 73 as its result. Similarly, ToDouble("73") would give you the double 73.0. Note that these will cause the program to crash if the data is not in the right format, we will look later at how to deal with this. For the moment, make sure you enter the data in the expected format.

SplashKit also provides convenient helper methods for this:

For simple conversions, mostly between different number formats, languages usually also provide an alternate option called type casting. With this you can ask the language to reinterpret data in a new format. This is achieved by placing the type within brackets (parenthesis) before the value to be converted. For example, (int)73.5 will give you the integer 70.

Example

Let’s revisit our table of example expressions, this time adding another column to show what data type each expression is.

Example ExpressionValueType
7373int
2.12.1double
"Hello World"”Hello World”string
"Fred"”Fred”string
3 * 26int
1 + 3 * 27int
(1 + 3) * 28int
7 - 3 + 15int
3 / 21int
3.0 / 2.01.5double
3 % 21int
11 % 32int
3 / 2.01.5double
1 + (3 / 2.0) + 6 * 2 - 86.5double
ToInt32("81")81int
ToDouble("27.5")27.5double
(int)27.527int

Activities

Identify the types of the following data. Where more than one type is possible, write down all the possible types. Where an expression is given, evaluate the expression and determine the type of the overall value.

  1. 392
  2. 32 * 3.9
  3. 40001
  4. "Hello World"
  5. 'm'
  6. "a" + 'c'
Answers
  • 1: This value has an int type in C#. It can be used where an int or short is required, but not where a byte is required.
  • 2: This is an expression that has an int literal and a double literal. It would evaluate to a double.
  • 3: This is an int type. It is too large to be a short or byte.
  • 4: This is a textual type, in this case a string.
  • 5: This is a textual type, in this case a char.
  • 6: The ’+’ operator acts as a concatenator for two string types, in this case a string with a char resulting in a string.

Video Overview and Demonstration of Types

In this video we’ll briefly look at what types are with some visual examples, then demonstrate how they behave by stepping through some simple code!