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 1005419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:17:30+00:00 2026-05-16T08:17:30+00:00

Okay, in python one can do this: def foo(monkeys): def bar(monkey): #process and return

  • 0

Okay, in python one can do this:

def foo(monkeys):
    def bar(monkey):
        #process and return new monkey
    processed_monkeys = list()
    for monkey in monkeys:
        processed_monkeys += bar(monkey)
    return processed_monkeys

(This is just a stupid example)
I sometimes miss this functionality of declaring a method inside another method in c#. But today I had the following idea for accomplishing this:

List<Monkey> foo(List<Monkey> monkeys)
{
    Func<Monkey, Monkey> bar = delegate(Monkey monkey)
    {
        //process and return new monkey
    }
    List<Monkey> processed_monkeys = new List<Monkey>(monkeys.Count);
    foreach(Monkey monkey in monkeys)
        processed_monkeys.Append(bar(monkey));
    return processed_monkeys;
}

Obviously the workaround does not provide exactly the same functionality as the original as the Func or Action Delegates are limited to 4 parameters, but more than 4 parameters are rarely needed…

What are your opinions on this?
Is this evil and should be avoided for all but very special situations?
How is the performance of this? Will a new bar function be created each time the function is called, or will the compiler optimize this somehow?

  • 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-16T08:17:31+00:00Added an answer on May 16, 2026 at 8:17 am

    …as the Func or
    Action Delegates are limited to 4
    parameters…

    Starting with .NET 4.0, these delegate types are defined for up to something like 17 parameters. You can also define your own quite simply for any arbitrary number of parameters; for example, below I define a delegate that takes 5 parameters:

    public delegate void Action<T1, T2, T3, T4, T5>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5);
    

    What are your opinions on this?

    It’s totally fine. Believe or not, .NET developers do this all the time.

    Is this evil and should be avoided for
    all but very special situations?

    Nope, it’s pretty benign. There’s really no harm in it.

    How is the performance of this? Will a
    new bar function be created each time
    the function is called, or will the
    compiler optimize this somehow?

    The performance is pretty good. The compiler will define an actual full-blown CLR type to provide the anonymous method, just like it defines full-blown CLR types for anonymous types and for methods using the yield keyword. Don’t worry; anonymous methods do not incur dynamic compilation on every invocation! Really, that would be pretty absurd, if you think about it.

    This isn’t even really what I’d call “optimization”; it’s simply the way that anonymous methods are implemented by the compiler.

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

Sidebar

Related Questions

Okay I am newer to python and have been researching this problem but I
Okay so this is what I'm trying to do in my python socket server.
Okay, so I have a bit of a problem, Im new to python sorry.
I installed Python 2.6 for one user on Windows Vista. Python works okay, but
okay code: #!/usr/bin/python import wx import sys class XPinst(wx.App): def __init__(self, redirect=False, filename=None): wx.App.__init__(self,
Okay, I'm totally new to Python, so I decided to make a simple app.
Okay, I'm really new with python and in my class the prof assigned a
Okay..the output of a python program is shown into a Tkinter window..which opens separately.
Okay. I have completed my first python program.It has around 1000 lines of code.
Possible Duplicate: The Python yield keyword explained Okay, I've probably phrased the question badly

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.