Delegates vs. Function Pointers, part 3: C# 1.0

This is part 3 in a series about state and function pointers; part 1 is here.

Last time, we saw that it is impossible to bundle context along with a function pointer in C.

In C#, it is possible to fully achieve my standard example.  In order to explain how this works behind the scenes, I will limit this post to C# 1.0 and not use a lambda expression.  This also means no LINQ, generics, or extension methods, so I will, once again, need to write the filter method myself.

delegate bool IntFilter(int num);

static ArrayList Filter(IEnumerable source, IntFilter filter) {
    ArrayList retVal = new ArrayList();

    foreach(int i in source)
        if (filter(i))
    return retVal;

class GreaterThan {
    public GreaterThan(int b) {
        this.bound = b;
    int bound;
    public bool Passes(int num) {
        return num > bound;

int x = 2;
int[] numbers = { 1, 2, 3, 4 };
Filter(numbers, new IntFilter(new GreaterThan(x).Passes));

Please excuse the ugly code; in order to be true to C# 1.0, I can’t just write an iterator, and I can’t implicitly create the delegate.

This code creates a class called GreaterThan that holds the Passes method and the state used by the method.  I create a delegate to pass to Filter out of the Passes method for an instance of the GreaterThan class from the local variable.

To understand how this works, we need to delve further into delegates.  .Net delegates are more than just type-safe function pointers. Unlike the function pointers we've looked at, delegates include state – the Target property. This property stores the object to pass as the hidden this parameter to the method (It's actually somewhat more complicated than that; I will describe open and closed delegates at a later date).  My example creates a delegate instance in which the Target property points to the GreaterThan instance.  When I call the delegate, the Passes method is called on the correct instance from the Target property, so it can read the bound field.

Next time, we'll see how the C# 2.0+ compilers generate all this boilerplate for you using anonymous methods and lambda expressions.


This curiosity you describe is fantastic Rita

The above article is nice and interesting, thank you willing to share! Greetings success of admin Percetakan Murah Rawamangun Jakarta Timur wish you deign to visit my website, thank you :)

I advise you to check out if you want to know more about computer science career. It will help you to achieve success

Thanks for the best share and i loved it,
cinema hd for windows10

Delegates vs. Function Pointers, part 3, is really great and valuable article for all the new users to developing their high interest in that but there are also some very great opportunities available like the most beneficial essay writing services which is very essential in giving their best and desirable writing services in cheap rates.

Nice post. You work very well. We provide custom boxes at wholesale rate. You can visit our site and get more information.
Custom Hair Extension Boxes Packaging UK

Beds Online: Buy Beds Online in india at best price. Shop from Customhouzz the best bed offering a wide range of king size bed, queen size bed, double bed, single bed and many more collection.

Buy Sharara Suits online for women. Parchhai is offering latest design sharara suits online in India for ladies/women, our product are perfect combination of quality price, what are you waiting for shop with us today and grab the exciting deals on your favourite design suits.

great work. I would love to know more about that. Meanwhile android has made entertainement much easy like you can watch movies and tv shows anytime and anywhere on cinema hd apk. you can easily download cinema hd apk on any device. Even firestick supports cinema hd how to install cinema hd apk on firestick.
download cinema apk is probably the best movie app for android users.

Thanks for the best share and i loved it, 192.168.o.1

These things they still say on social networks yo whatsapp of

Players contact the display screen to open a package deal and acquire a bonus payout. They could hold touching packages for extra bonuses till one package deal lastly reveals a "pooper," which ends the spherical. These bonus rounds helped video slots grow in popularity, but they don't seem to be} as prevalent right now, as recreation designers experiment with format. A generous welcome bonus is a should 온라인 카지노 for any respected online casino.

Great blog is a leading provider of online assignment help in Greece, catering to the needs of students from different academic backgrounds. Writers can provide you with customized solutions that meet your specific academic requirements.

Thanks for your beautiful content. I am enjoyed to read this information and learned a lot. Thank you so much for your good content.
proceso de divorcio de nueva jersey

Post a Comment