A Common Source of Bugs

When an operation has to be invoked on an instance of such a reference type, a dereferencing operation occurs. It’s invalid, though, to dereference the null reference, causing an exception to occur. Figure 4.37 shows such a NullReferenceException.

Image

FIGURE 4.37 A bad encounter with a null reference.

Null references that are unaccounted for are probably the number one source of bugs in managed code programming. Rigorous checking for null references is an important pattern everyone should master. Obviously, the question is where such checks need to be applied.

When designing a new library, it’s essential to specify the intended ...

Get C# 5.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.