Posted on Tuesday, February 19, 2008 at 6:23 AM
Recently, a friend mentioned that someone had told him that writing a try...catch block in C# (or substitute some other .Net language here) resulted in a "huge penalty" in terms of performance compared to if you had not written it. That is, merely writing such a block actually hurt program performance, even if an exception was never thrown. He didn't believe this was true, and rightly so - it's completely wrong. This post is a tidied up version of my explanation.
Inside a .Net Assembly
A .Net assembly, if we ignore various headers, consists of three things:
- Bytecode: a sequence of low-level instructions that specify the body of a method
- Metadata: a set of tables, a little bit like a database, describing higher level constructs such as classes, methods, signatures and so forth
- Heaps: places where string constants and other such things are stored...