4.1.1.1 - Data types and references (C#)

4.1.1.1 - Data types and references (C#)

A program needs to know what a value means before it can store or process it. Learn how C# types describe values, how references connect variables to objects, and how arrays and user-defined records organise related data.

C# route status: incomplete: runnable support pending. The examples and code answers in this route are written C#; they have no Run control. Paper 1 is an onscreen programming examination, so this route still needs runnable support for complete practical preparation.

Built-in types and representation

A type determines the values a variable can represent and the operations that make sense on them.

Data type

A classification of data that specifies its possible values and supported operations.

For example, adding two quantities is meaningful, while adding two names normally means joining text. Choosing a type also prevents some mistakes: C# cannot assign the string "late" directly to an int variable.

Required ideaC# exampleAppropriate use
Integerint attempts = 3;A whole-number count
Real/floatdouble temperature = 18.75;A measurement with a fractional part
Booleanbool doorOpen = false;A true/false state
Characterchar grade = 'B';One UTF-16 character code unit
Stringstring name = "Mina";A sequence of text characters
Date/timeDateTime meeting = new DateTime(2027, 5, 12, 9, 30, 0);A date and clock time

The DateTime example requires using System;. Single quotes surround a char; double quotes surround a string. "3" is text, whereas 3 is an integer. They look similar when displayed but behave differently in calculations.

int, double, bool, char and DateTime are value types: assignment copies their value. int has a finite range; double represents many real-number approximations, not every mathematical real exactly. decimal is a useful built-in alternative for decimal money calculations; an m suffix creates a decimal literal, such as 12.50m. Do not assume a real measurement or a money amount should be stored as an integer merely because one example happens to be whole.

References and dynamic objects

A reference lets a variable refer to an object created while the program is running.

At the conceptual memory level, a pointer/reference stores an address or a managed reference identifying another object. C# normally uses managed references rather than exposing numerical addresses. new creates an object dynamically; the runtime manages its storage. Understanding the relationship is required even without writing unsafe pointer code.

Arrays are reference types. Trace this fragment inside Main:

C#
int[] first = new int[] { 4, 8 };
int[] second = first;
second[0] = 9;
int value = first[1];
int copy = value;
copy = 20;

After second = first, both variables refer to the same array. No second array has been made. Updating second[0] therefore changes first[0] to 9. In contrast, copy = value copies the integer 8; assigning 20 to copy leaves value at 8.

VariableFinal value or referenced contents
first, secondThe same array containing { 9, 8 }
value8
copy20

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account