O'Reilly logo

C# 5.0 Unleashed by Bart De Smet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Delegate Types

As you’ve seen repeatedly, the Common Language Runtime (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++:

int add(int a, int b) {    return a + b;}void main() {    int (*fpAdd)(int, int);    fpAdd = &add;    int three = fpAdd(1, 2); // calls the add function through the pointer}

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required