70-536: Exception classes

19 11 2005

The .NET runtime supports structured exception handling. As part of this mechanism there is a hierarchy of exception classes to help identify the particular error that has occured. The base of the class hierarchy is the System.Exception object.

Back in .NET 1.x days it was suggested that all custom exceptions created by application developers be derived from the System.ApplicationException class but that advice has since been changed and we should derive directly from System.Exception.

One thing to watch out for with exceptions is serialisation issues. If you are passing the object between AppDomains on the local machine or across the network exceptions won’t necessarily serialise correctly so you basically need to implement the ISerializable interface. One of the best articles on this subject remains Eric Gunnerson’s “The Well-Tempered Exception”.





70-536: Generic types

19 11 2005

Generic types are one of the cool new features in the .NET 2.0 runtime. Basically they allow you to construct a type passing in a type parameter which customises the generic type on the fly to support that type specifically. A really good example is the generic collection classes like List<T> which allow you to create a list of the specific objects you want to deal with.

When you construct a generic type for a reference type the runtime shares the underlying code base because it is just dealing with pointers anyway – it just makes it look like a different type, however for value types it generates a unique code base for each value type.





70-536: Attributes

19 11 2005

Attributes are a way of attaching meta data to different runtime elements such as assemblies, types, methods, properties, fields and even arguments! All attributes ultimately derive from the System.Attribute base class and also have some attributes applied to them (AttributeUsage) to control how that attribute can be applied to code.

There are also different types of attributes, for example, there are attributes that result in changes to the compiled output at compile time (like the PrincipalPermission attribute), attributes which the runtime refers to when it is inflight – such as the ContextAttribute, and then there are the attributes that are pure meta data – such as the WebMethodAttribute.

While attributes are cool – they do incur some runtime overhead and it can sometimes be more performant to tag with interfaces rather than tagging with attributes.





70-536: Reference types

19 11 2005

Given that I have already covered value types - this should be pretty quick. Reference types have the following characteristics:

  • They are allocated on the heap.
  • There is a pointer on the stack which points to the location on the heap.
  • The .NET runtime automatically dereferences reference types.
  • Reference types are copied by reference.
  • You can _sometimes_ use the Clone method to get a deep or shallow copy of an object but it is by no means guaranteed.
  • When you pass a reference type by reference you end up with a double pointer, and the runtime still automatically dereferences it for you!

That should just about cover it





70-536: Value types and nullable types.

19 11 2005

So briefly, lets look at a few of the defining features of value types:

  • Allocated on the stack.
  • Have a default value (newobj not required).
  • Are typically pretty small (integer types, booleans etc)
  • Are copied by value by default.

The reason the runtime distinguishes between value types and reference types is performance. A value type is much faster to work with because the runtime doesn’t have the dereference them to get at the data.

Now you may be wondering why they don’t make everything a value type – well, the act of doing that would make them slow. The stack is a small efficient pocket of memory that the runtime can get at quickly. If you put everything on the stack it would become a heap!

Boxing and Unboxing

It is possible for a value type to live on the heap, and the processing of taking it on and off of the heap is called boxing and unboxing respectively. Boxing and unboxing is pretty expensive, but it was hard to avoid without a lot of work in .NET 1.x because the default collection classes really only dealt with objects – if you wanted to be more efficient you had to build your own collection and down your own internal array resizing etc.

Nullable Types

I said that value types always have a default value, and thats true, but in .NET 2.0 they introduced a new generic type called Nullable<T> which basically allows us to set a value type to a null value. This kind of thing would be most valuable on the data path for an application where the underlying application database supports the concept of nullability for tradtional .NET value types.

C# 2.0 provides us with a shortcut syntax where we can just append a ? to a value type variable definition to say it is nullable, and we can also use the ?? operator to allow us to set a default value if the variable being worked on happens to be set to null.

When it comes to evaluating expressions with nullable types the general rule is that if one of the operands is null, the result is null. However, when it comes to working with the & and | operators and a nullabel boolean the story is a little bit different.

This MSDN reference has a good table to look at this specifically – but in general the logic conforms to how we would expect SQL to behave.





On the road to certification . . .

19 11 2005

With the release of Visual Studio 2005 it is time to start looking at updating my certifications. It should be quite interesting this time around because the structure of the Microsoft Certified Professional program has changed quite a bit. You can check out the new structure over at the Microsoft Certified Professional Developer homepage.

Basically they have split the program into two parts, there are a set of technology specialisations (MCTS certifications) and then a set of job focused specialisations (MCPD certifications). I’ll probably end up going for all of the technology specialisations first and then onto the job focused ones in due time.

All of the technical specialisations require you to pass exam 70–536 or “Microsoft .NET Framework 2.0 – Application Development Foundation”. If you look at the skills being measured it is pretty stock standard BCL stuff. As part of my preparation (just waiting to get signed up to the BETA exam) I am going to bash out a few posts about the skills being measured just to see what I know.





70-536: ArrayList class

19 11 2005

The System.Collections.ArrayList class was the work horse of the .NET 1.x runtime when it came to managing data. It wasn’t necessarily the most efficient thing for dealing with value types but it sure beat building your own list type from scratch.

The ArrayList class supports dynamically add or removing items from the list and can contain any kind of object.





70-536: Forwarding types?

19 11 2005

What the? Do you have any idea what they mean by forwarding types? I probably know it as a different name, or they are just trying to confuse me!





70-536: Boxing and Unboxing

19 11 2005

I kind of covered this one off when I covered value types. Boxing occurs when you put a value type on the heap and it is typically an expensive operation. Unboxing occurs when you take a value type that was previously boxed and put it back onto the stack.

Boxing and unboxing look suspiciously like casting in code but the underlying IL instructions that get generated are different – check it out for yourself with ILDasm!