Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

169
Views
Any one know how exactly IL/CLR generates Local Functions C#7

https://www.infoworld.com/article/3182416/c-7-in-depth-exploring-local-functions.html

I really need to know exactly following code how Local Functions supposed to be.

Am 0% IL Code/MSIL experience.

  public static void Display(string str)
    {
        int ctr = 5;
        DisplayText();

        void DisplayText ()
        {
            for(int i = 0; i < ctr; i++)
            Console.WriteLine(str);
        }
    }

Question I need to know here from previous code:

  1. If we call main function Display() does Local Function DisplayText() always generated when main one called? Or it created as a fake local function in C# but in MSIL it generated as Global function?
  2. In lamda expression method? does it same above?
  3. Is it safe to depend on local functions? or shall we not used it anyway. (performance maybe idk)

Edit:- I think Local Properties (which not exists yet or maybe ...). it is useful also sometimes. which you can have some calculation in same Method as a local property. as previous example but DisplayText are property. Wish they add it also.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

CIL and the runtime have no concept of a local function, instead the C# compiler translates it into a normal method with private visibility and potentially a class/struct used to share state with the declaring method. You can see this for yourself by compiling the code and then decompiling the assembly, or by using a site such as SharpLab.io.

Lambda expressions and anonymous methods are very similar, except that they need to provide a delegate which can limit the optimization opportunities.

The compiler puts a lot of effort into optimizing the generated code, I would not expect local methods to affect performance much beyond what you would get for any other method. I would suggest you focus more on how it affects maintainability, at least until you can identify an actual performance problem by profiling.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!