O'Reilly logo

.NET Common Language Runtime Unleashed by Kevin Burton

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

Why delegates?

In the past, you could pass a function pointer, cast as an int, and it was up to the function being called to know what type of function pointer was passed. This improved somewhat with later versions of C where the return type of function, number of arguments, and argument type could all be specified. The problem is that it was not type-safe. One example that is used frequently is qsort. This function was passed a void pointer to what was to be sorted, the number of elements to be sorted, the size of the elements, and a comparison function. The call to qsort when you are sorting an array of int might look like this:

int a[100]
// Initialize array a
. . .
qsort((void *)a, 100, sizeof(int), compare);

The comparison routine would ...

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