Skip to content

.NET Framework Fundamentals

by Alex Peck on July 1st, 2009

Some time ago I began studying for the .NET Framework 2.0 Application Development Foundation exam. In order to cement the aquired knowledge I have decided to post my notes as I recap, starting at the absolute beginning.

Types

For some time I didn’t fully appreciate the difference between reference and value types. The following table summarises the important characteristics:

Value Types Reference Types
Actually contain data. Assignment copies data. Store a reference to the real data, assignment copies the reference.
Local value types reside on the stack.* Reference resides on the stack, the actual data on the heap.
Derived (implicitly) from System.ValueType Derived from System.Object, or a descendant
User value types are structs or enums. Unlike in C, you can define methods on structs. May be a class, interface, delegate or an array (including arrays of value types).
Implicitly sealed

* Local as in declared within a method. When allocating a class with a value type member, this member will be allocated on the heap.

Boxing and Unboxing

A boxing operation converts a value type to a reference type; unboxing converts a reference type to a value type. For example, converting an int to an object (boxing) causes a new int to be created on the heap, the object reference then refers to this copy. Clearly, this incurs some overhead.

Generics

Generics are paramaterised types and methods, and are superficially similar to C++ templates. The most common use of generics in the .NET framework are the collection classes introduced in .NET 2.0.

Constraints may be specified to restrict use of generic types and methods:

where T : struct any value type, excluding Nullable types
where T : class any reference type (class, interface, delegate or array)
where T : new T must have a public parameterless constructor
where T : <base> T must derive from base, which can be either a class or interface. Multiple interfaces may be specified, and they may also be generic.

Attributes

Attributes are programmatic annotations (metadata) that may be queried by reflection. An item decorated with an attribute can be an assembly, class, constructor, delegate, enum, event, field, interface, method, portable executable file module, parameter, property, return value, struct, or another attribute. Attributes derive from System.Attribute.

The TypeForwardedTo attribute allows you to move a type from one assembly to another without having to recompile the application which uses the original assembly. This is succinctly illustrated here.

Partial Classes

Partial Classes facilitate splitting class definitions across files. I find this is useful when using code generation – you can re-generate an entire class file from a model without overwriting hand written code.

Exceptions

The training material recommends that you define your own exceptions which derive from System.ApplicationException. It is therefore possible to differentiate between exceptions thrown from application code and the .NET framework.

In reality, it is best practice to simply inherit from Exception, as ApplicationException didn’t add any value.

Either way, having distinct exception classes allows you to respond to different exceptions differently. Since it is possible to catch a derived exception by catching the base type, catch blocks should be ordered from most to least specific.

The finally block should be used to dispose of non memory resources, e.g. closing files or a SqlConnection.

No comments yet

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS