Chapter 2
Types and Exceptions

Before you begin drilling down into the Microsoft .NET Framework class library (FCL) and the various programming models that it supports, it鈥檚 helpful to understand what the FCL is made of. The FCL is a library of 鈥渢ypes,鈥?which is a generic way of referring to classes, structs, interfaces, enumerations, and delegates. This chapter defines these terms and will make Chapter 3 more meaningful to developers who are new to the .NET Framework. This chapter also introduces some potential pitfalls related to types, including common errors that arise when using types that encapsulate file handles and other resources that aren鈥檛 managed by the garbage collector.

Understanding the .NET Framework鈥檚 type system and the differences between the various kinds of data types that it supports is important, but so is understanding how types are loaded, versioned, and deployed. Types are packaged in assemblies. The FCL is a set of many different assemblies, each one of them shared so that any application can use it. Applications, too, are deployed as assemblies. You already know that an assembly is a group of one or more files. You even created a single-file assembly (Hello.exe) in Chapter 1. What you don鈥檛 know鈥攜et鈥攊s how to create assemblies of your own that contain types that can be used by other programs. You鈥檒l remedy that in this chapter by building and deploying a multifile, multilanguage assembly, and then building a client that dynamically links to it. In addition to gaining valuable insight into how the FCL works, you鈥檒l see what it takes to build class libraries of your own and learn how to use the assembly-based versioning mechanism in the common language runtime (CLR) to avoid DLL Hell鈥攖he term used to describe what happens when a fix made to a DLL for the benefit of one application breaks another application (or perhaps breaks the very application that the fix was intended to help).

Finally, you鈥檒l learn about exception handling. Applications that use the .NET Framework employ a C++-like try/catch mechanism to achieve robustness without having to check the return value from each and every method call. In fact, checking return values does little good because the CLR and FCL don鈥檛 flag errors by returning error codes; they throw exceptions. A working knowledge of how exception handling works and how languages such as C# expose the CLR鈥檚 exception handling mechanism is essential to becoming a proficient .NET Framework programmer.