[ Home | Syllabus | Course Notes | Assignments | Search]
The C in FCL stands for “class,” but the FCL isn’t strictly a class library; it’s a library of types. Types can mean any of the following:
These are derived from a base class called "Object" as shown in this picture
Framework classes can contain the following members:
Here, in C#, is a class that implements a Rectangle data type:
class Rectangle { // Fields protected int width = 1; protected int height = 1; // Properties public int Width { get { return width; } set { if (value > 0) width = value; else throw new ArgumentOutOfRangeException ( "Width must be 1 or higher"); } } public int Height { get { return height; } set { if (value > 0) height = value; else throw new ArgumentOutOfRangeException ( "Height must be 1 or higher"); } } public int Area { get { return width * height; } } // Methods (constructors) public Rectangle () {} public Rectangle (int cx, int cy) { Width = cx; Height = cy; } }
Rectangle has seven class members:
two fields
three properties (height, width, area)
two methods, which both happen to be constructors
Notice that these
properties’ set accessors throw an
exception if an illegal value is entered, a protection that couldn’t be
afforded had Rectangle’s width and height
been exposed through publicly declared fields.
Area
is a read-only property because it lacks a set
accessor.
Many languages that target the .NET Framework feature a new operator for instantiating objects. The following statements create instances of Rectangle in C#:
Rectangle rect = new Rectangle (); // Use first constructor Rectangle rect = new Rectangle (3, 4); // Use second constructor
Once the object is created, it might be used like this:
rect.Width *= 2; // Double the rectangle's width int area = rect.Area; // Get the rectangle's new area
"reference" types are allocated on the heap - which is managed by a garbage collector
"value" types are allocated on the stack - which is managed by functional scoping
value types are defined with the struct keyword
The finalize method is called by the garbage collector before destroying an object on the heap - this is needed to explictely "free" unmanaged resources (like file and window handles)
the "new" operator can allocate "value" types as well
Finalize method makes no sense with value types
Can Box (convert a value type to a reference type) and unbox (convert reference type to a value type)
reference types must use a new operator to create an instance of an object
Must also be aware of shallow copies of reference types
non deterministic destruction due to garbage collection
NOTE global and static variables live in yet another space
Byte (8 bits)
Char(16 bits)
Int16/32/864
UInt16/32/64
Single/Double
Decimal
Enum
Boolean
Structure (contains methods implements interfaces) - cannot be base "class"
Class: Methods, events, properties
Interface: ,methods, properties and events
Array: inherit from System.Array - so "safe"
String: Unicode - once created cannot be changed
Delegate: type safe pointer to a method
Characteristics
abstract: no implementation supplied
final: cannot be overridden
virtual: supports polymorphism (run time determination)
static: belongs to class not object instance
private/family/public: controls access
Problems with dynamic memory (among the most difficult to discovery)
Memory Leak
Can easily go undetected for years
int *ptr = new int[1000000];
ptr = 0;
Unauthorized access to memory
These are legal c++ statements
These faults may or may not lead to detectable failures
int i, *ptr;
ptr = &i;
*(ptr+37) = 6;
int a[100];
a[-37] = 7;
dangling pointers
int *ptr = new int[100];
delete []ptr;
*ptr = 5;
A c++ program which illustrates these common misuses of dynamic memory can be found here http://www.cs.odu.edu/~wild/cs333/fall02/assignments/assn4.htm
Need "safe" pointers, arrays no delete statements and automatic garbage collection
BUT garbage collection is expensive
A typical program can create a lot of "temporary" variables, eg
int f(int a)
{
int b = a*a;
return b;
}
//calling "f"
int y = f(4);
How many "temporary variables" are there - answer 3
copy value of "4" into "a" - allocated on stack
variable "b" is allocated on stack
"return b" creates a temporary unnamed variable used to pass back information
If subject to garbage collection, lots of overhead.
.NET storage architecture is a compromise between safety and efficiency
Instead of multiple inheritance - .Net supports Interfaces which are abstract classes
There can be any number of "interface"s that a class is "derived" from
'"is" operator tests if a class supports an interface
"as" casts the object into an interface object
Enumerations support GetNames
A Delegate is a callback function
MetaData describes the types contained in a module:
Type's name
Visibility (public/assembly)
Base class
interfaces implemented
methods/properties/events
This is the information used by IDLASM
Manifest contains information about all modules (DLLs and EXEs)
Metadata about the assembly
Name
version number
culture
list of files
dependencies (with version numbers)
Used for scoping types
does not use the registry
Can have a "strong" name which includes a digital signature
assemblies can be located across a network
DLLs require run-time hosts (such as provided by ASP.NET and Internet Explorer)
An AppDomain is like a process or thread
See program 2 here