Fundamentals of Computer Programming with C#
132 Fundamentals of Computer Programming with C#
string firstName = "John";
ch = (char)5;
// Here we use an already initialized variable and reassign it
secondChar = ch;
Value and Reference Types
Data types in C# are two types: value and reference.
Value types are stored in the program execution stack and directly contain
their value. Value types are the primitive numeric types, the character type
and the Boolean type: sbyte, byte, short, ushort, int, long, ulong,
float, double, decimal, char, bool. The memory allocated for them is
released when the program exits their range, i.e. when the block of code in
which they are defined completes its execution. For example, a variable
declared in the method Main() of the program is stored in the stack until the
program completes execution of this method, i.e. until it finishes (C#
programs terminate after fully executing the Main() method).
Reference types keep a reference (address), in the program execution
stack, and that reference points to the dynamic memory (heap), where
their value is stored. The reference is a pointer (address of the memory cell)
indicating the actual location of the value in the heap. An example of a value
at address in the stack for execution is 0x00AD4934. The reference has a
type. The reference can only point to objects of the same type, i.e. it is a
strongly typed pointer. All reference types can hold a null value. This is a
special service value, which means that there is no value.
Reference types allocate dynamic memory for their creation. They also
release some dynamic memory for a memory cleaning (garbage
collector), when it is no longer used by the program. It is unknown exactly
when a given reference variable will be released of the garbage collector as
this depends on the memory load and other factors. Since the allocation and
release of memory is a slow operation, it can be said that the reference types
are slower than the value ones.
As reference data types are allocated and released dynamically during
program execution, their size might not be known in advance. For example, a
variable of type string can contain text data which varies in length. Actually
the string text value is stored in the dynamic memory and can occupy a
different volume (count of bytes) while the string variable stores the address
of the text value.
Reference types are all classes, arrays and interfaces such as the types:
object, string, byte. We will learn about classes, objects, strings, arrays
and interfaces in the next chapters of this book. For now, it is enough to know