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
, and37
. - Real numbers such as
0.5
,-126.0
,3.141516
and23.981
.
The concept map below shows the concepts related to types.
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.
Name | Size |
---|---|
char | 2 bytes/16 bits |
string | variousa |
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 | Size | Range (lowest .. highest) |
---|---|---|
byte | 1 bytes/8 bits | 0 .. 255 |
short | 2 bytes/16 bits | -32,767 .. 32,767 |
int | 4 bytes/32 bits | -2,147,483,648 .. 2,147,483,647 |
long | 8 bytes/64 bits | -9,223,372,036,854,775,807 .. 9,223,372,036,854,775,807 |
uint | 4 bytes/32 bits | 0 to 4,294,967,295 |
ulong | 8 bytes/64 bits | 0 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.
Name | Size | Range (lowest .. highest) | Significant Digits |
---|---|---|---|
float | 4 bytes/32 bits | ±1.5 x 10−45 to ±3.4 x 1038 | ~6 to 9 |
double | 8 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 data | Operations 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 astring
to anint
.ToDouble
- this can convert astring
to adouble
.
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:
- ConvertToInteger - can be used to convert a string to an integer.
- ConvertToDouble - can be used to convert a string to a double.
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 Expression | Value | Type |
---|---|---|
73 | 73 | int |
2.1 | 2.1 | double |
"Hello World" | ”Hello World” | string |
"Fred" | ”Fred” | string |
3 * 2 | 6 | int |
1 + 3 * 2 | 7 | int |
(1 + 3) * 2 | 8 | int |
7 - 3 + 1 | 5 | int |
3 / 2 | 1 | int |
3.0 / 2.0 | 1.5 | double |
3 % 2 | 1 | int |
11 % 3 | 2 | int |
3 / 2.0 | 1.5 | double |
1 + (3 / 2.0) + 6 * 2 - 8 | 6.5 | double |
ToInt32("81") | 81 | int |
ToDouble("27.5") | 27.5 | double |
(int)27.5 | 27 | int |
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.
392
32 * 3.9
40001
"Hello World"
'm'
"a" + 'c'
Answers
- 1: This value has an
int
type in C#. It can be used where anint
orshort
is required, but not where abyte
is required. - 2: This is an expression that has an
int
literal and adouble
literal. It would evaluate to adouble
. - 3: This is an
int
type. It is too large to be ashort
orbyte
. - 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 achar
resulting in astring
.
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!