Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 231159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:54:36+00:00 2026-05-11T19:54:36+00:00

I Just can’t seem to wrap my head around them. As I understand it’s

  • 0

I Just can’t seem to wrap my head around them.

As I understand it’s dynamically adding logic to a class. Are classes within the framework prepared for this?

Why should I just extend the class and add the functionality to it in the extension. I would be globally accessible and afaik much easier to maintain.

I’ve Read there are 4 functor types:

Comparer
Closure
Predicate
Transformer

We should probably Handle each one of them.

p.s. is there something like it in vb?

So I can state I think that lambda expressions are functors. This clears up things for me a bit 🙂 (hehe)

  • Lambda expressions are functors?
  • Anonymous functions are functors?

But I asked this question because I ran into another type of fucntors namely these ones:

delegate void FunctorDelegate(int value);
class Addition {
    FunctorDelegate _delegate;

    public Addition AddDelegate(FunctorDelegate deleg) {
        _delegate += deleg;
        return this;
    }
    public int AddAllElements(IList< int> list) {
        int runningTotal = 0;
        foreach( int value in list) {
            runningTotal += value;
            _delegate(value);
        }
        return runningTotal;
    }
}

And then calling it with this:

 int runningTotal = new Addition()
     .AddDelegate(new FunctorDelegate(
                     delegate(int value) {
                         if ((value % 2) == 1) {
                             runningOddTotal += value;
                         }
                     }))
    .AddDelegate(new FunctorDelegate(
                     delegate(int value) {
                         if ((value % 2) == 0) {
                             runningEvenTotal += value;
                         }
                     }))
    .AddAllElements(list);

So no fancy lambda style things.

Now I have this example but it isn’t at all clear why this is a “good” solution.

Are delegates (functors) used as lambda expressions or anonymous methods “in most cases” just there as a shortcut for the programmer? There are as far as I can see only a few cases where they’re actually the prefered choice for a problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T19:54:36+00:00Added an answer on May 11, 2026 at 7:54 pm

    I think you’re confusing terms from different languages. You seem to be using “Functor” in the C++ or Java sense, e.g. see the wikipedia page. In C++, it’s an object of a class that overloads the function-call operator, so it can be used as a function but with state.

    This is logically the same thing as a delegate bound to an instance method in C# (or any .NET language).

    There are three ways to write such a thing. First, you can write an ordinary method, and then assign the name of the method to a delegate variable.

    void MyMethod() { Console.WriteLine("Hi!"); }
    
    void Foo()
    {
        Action a = MyMethod;
        a();
    }
    

    Second, you can use anonymous method syntax, introduced in C# 2.0:

    void Foo()
    {
        Action a = delegate { Console.WriteLine("Hi!"); }
        a();
    }
    

    Thirdly, you can use lambda syntax, introduced in C# 3.0:

    void Foo()
    {
        Action a = () => Console.WriteLine("Hi!");
        a();
    }
    

    The advantage of the last two is that the body of the method can read and write local variables in the containing method.

    The advantage of lambda syntax over anon-methods are that it is more succinct and it does type inference on parameters.

    Update: The advantage of anon-methods (delegate keyword) over lambdas is that you can omit the parameters altogether if you don’t need them:

    // correct way using lambda
    button.Click += (sender, eventArgs) => MessageBox.Show("Clicked!");
    
    // compile error - wrong number of arguments
    button.Click += () => MessageBox.Show("Clicked!");
    
    // anon method, omitting arguments, works fine
    button.Click += delegate { MessageBox.Show("Clicked!"); };
    

    I know of only one situation where this is worth knowing, which is when initializing an event so that you don’t have to check for null before firing it:

    event EventHandler Birthday = delegate { };
    

    Avoids a lot of nonsense elsewhere.

    Finally, you mention that there are four kinds of functor. In fact there are an infinity of possibly delegate types, although some authors may have their favourites and there obviously will be some common patterns. An Action or Command takes no parameters and returns void, and a predicate takes an instance of some type and returns true or false.

    In C# 3.0, you can whip up a delegate with up to four parameters of any types you like:

    Func<string, int, double> f;  // takes a string and an in, returns a double
    

    Re: Updated Question

    You ask (I think) if there are many use cases for lambdas. There are more than can possibly be listed!

    You most often see them in the middle of larger expressions that operate on sequences (lists computed on-the-fly). Suppose I have a list of people, and I want a list of people exactly forty years old:

    var exactlyForty = people.Where(person => person.Age == 40);
    

    The Where method is an extension method on the IEnumerable<T> interface, where T in this case is some kind of Person class.

    This is known in .NET as “Linq to Objects”, but known elsewhere as pure functional programming on sequences or streams or “lazy” lists (all different names for the same thing).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just can't seem to wrap my mind around this concept... I want to
Just can't wrap my head around the proper syntax for this one. Below is
I just can't seem to get localization to work. I have a class library.
I just can't get my head around how this is supposed to work: As
I just can't get my head around why I can't get the below shown
I just can't get my head around this and possibly my title isn't perfectly
I just can't get around my head about AUTO_CREATE_STATISTICS? I run the tuning advisor
I just can't get my head around it. What do I need to write
I just can't understand why is my database (mysql) behaving like this! My console
I just can't get the ajax service to work. A simple class to $.get(http://google.com)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.