4-C# Data Types, Objects and References
4-C# Data Types, Objects and References
Employee.cs
TimeSheet.cs
Program.cs
RecordOvertime method
finishes
Variables removed from
stack
What is actually stored?
Example:
hours parameter in call to
AddEntry
Integer value
Memory to hold an integer
value is allocated on the
stack
Actual integer value stored
on stack
What is actually stored?
Another example:
emp1 variable in Main, of type Employee
Points to an Employee object
Stack Heap
ts: TimeSheet
emp2:Employee
emp1:Employee
loc: Location
Employee
Program.Main
Object references
Memory to store attributes of object is
allocated on heap
The stack just stores a pointer, or
reference, to the object
The reference is essentially an address in
heap memory which the program can go to in
order to find the correct object
Object references
Main creates several objects on the heap and
references to these on the stack
Stack Heap
TimeSheet
Employee
ts: TimeSheet
emp2:Employee
Employee
emp1:Employee
loc: Location
Program.Main
Location
Parameter passing
RecordOvertime passes integer value hours
to AddEntry
Copy is made of integer value and added to
stack as parameter of AddEntry
References to same object
Main creates TimeSheet object reference and
passes it as a parameter to RecordOvertime
Parameter contains a copy of the reference
There can be many references in the stack to a
single object in the heap
Stack Heap
isWeekend: bool
hours: int
timeSheet: TimeSheet TimeSheet
copy Employee.RecordOvertime
ts: TimeSheet
emp2:Employee
emp1:Employee
loc: Location
Program.Main
Releasing memory
If a program keeps allocating memory and
never releases it for re-use, the memory will
fill up and the computer will crash
Stack memory is released whenever a
method finishes
The memory allocated for that method is
removed from the stack and is available for
re-use
Garbage collection
Releasing heap memory is more complicated
As long as there is at least one reference to
an object on the heap then the object is kept
“alive”
Objects with no references are eligible to be
removed and their memory released
Removed by the garbage collector
Employee objects in the example will be
removed when Main method finishes
Garbage collection
Garbage collector (GC) runs periodically and
removes “orphaned” objects
Can’t be sure exactly when it will do so
GC is a feature of a managed language
e.g. .NET languages, Java, PHP
Programmer does not have to manage memory
Unmanaged languages require programmer
to explicitly release memory
e.g. C++, C
Value types and reference
types
The .NET type system defines two categories of
data type, or object type
Value types
Values can be stored on the stack
Derived from System.ValueType
Examples of built-in framework value types:
Byte, Int16, Int32, Int64, Single, Double, Decimal, Char,
Boolean
C# has built-in types which are aliases for these:
byte, short, int, long, float, double, decimal, char, bool
Value types and reference
types
Reference types
Objects stored in the heap
References stored on the stack
Types derived from System.Object
Examples of reference types:
String (C# alias is string)
all classes, including classes in your project
Unboxing
Converting reference type to value type
on stack emp2:Employee
emp1:Employee
loc: Location
Program.Main
struct vs. class
TimeSheet example is a small struct, but
structs can have large numbers of instance
variables
Passing large structs as parameters can use a
lot of stack memory
On the other hand, creating objects on the
heap is expensive in terms of performance
compared to creating structs
No definitive rules, but take these factors
into account when deciding
enum
enum is a good way of storing and naming
constant values
Employee.cs
Program.cs
enum example
Previously indicated pay rate with boolean
value isWeekend
Replace this with enum, which allows more
than simply true/false
enum example
Change parameter in RecordOvertime to type
PayRate
enum example
Pass in enumeration value to method