Delegate Types

As you’ve seen repeatedly, the CLR provides type safety, making programs more robust against errors introduced by programmers. It’s no surprise that delegates are no exception to this rule. This immediately contrasts delegate types against raw function pointers as seen in languages such as C and C++:

image

Although the preceding fragment is well written, lots of opportunities exist to perform unsafe operations. For example, you could trick fpAdd so that it points to a function that takes arguments of types different from int (or a different number of arguments), without any runtime protection whatsoever against this kind of mishap. ...

Get C# 4.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.