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

  • Home
  • SEARCH
  • 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 560847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:23:14+00:00 2026-05-13T12:23:14+00:00

Following this question – Pass Method as Parameter using C# and some of my

  • 0

Following this question – Pass Method as Parameter using C# and some of my personal experience I’d like to know a little more about the performance of calling a delegate vs just calling a method in C#.

Although delegates are extremely convenient, I had an app that did lots of callbacks via delegates and when we rewrote this to use callback interfaces we got an order of magnitude speed improvement. This was with .NET 2.0 so I’m not sure how things have changed with 3 and 4.

How are calls to delegates handled internally in the compiler/CLR and how does this affect performance of method calls?


EDIT – To clarify what I mean by delegates vs callback interfaces.

For asynchronous calls my class could provide an OnComplete event and associated delegate which the caller could subscribe to.

Alternatively I could create an ICallback interface with an OnComplete method that the caller implements and then registers itself with the class that will then call that method on completion (i.e. the way Java handles these things).

  • 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-13T12:23:14+00:00Added an answer on May 13, 2026 at 12:23 pm

    I haven’t seen that effect – I’ve certainly never encountered it being a bottleneck.

    Here’s a very rough-and-ready benchmark which shows (on my box anyway) delegates actually being faster than interfaces:

    using System;
    using System.Diagnostics;
    
    interface IFoo
    {
        int Foo(int x);
    }
    
    class Program : IFoo
    {
        const int Iterations = 1000000000;
    
        public int Foo(int x)
        {
            return x * 3;
        }
    
        static void Main(string[] args)
        {
            int x = 3;
            IFoo ifoo = new Program();
            Func<int, int> del = ifoo.Foo;
            // Make sure everything's JITted:
            ifoo.Foo(3);
            del(3);
    
            Stopwatch sw = Stopwatch.StartNew();        
            for (int i = 0; i < Iterations; i++)
            {
                x = ifoo.Foo(x);
            }
            sw.Stop();
            Console.WriteLine("Interface: {0}", sw.ElapsedMilliseconds);
    
            x = 3;
            sw = Stopwatch.StartNew();        
            for (int i = 0; i < Iterations; i++)
            {
                x = del(x);
            }
            sw.Stop();
            Console.WriteLine("Delegate: {0}", sw.ElapsedMilliseconds);
        }
    }
    

    Results (.NET 3.5; .NET 4.0b2 is about the same):

    Interface: 5068
    Delegate: 4404
    

    Now I don’t have particular faith that that means delegates are really faster than interfaces… but it makes me fairly convinced that they’re not an order of magnitude slower. Additionally, this is doing almost nothing within the delegate/interface method. Obviously the invocation cost is going to make less and less difference as you do more and more work per call.

    One thing to be careful of is that you’re not creating a new delegate several times where you’d only use a single interface instance. This could cause an issue as it would provoke garbage collection etc. If you’re using an instance method as a delegate within a loop, you will find it more efficient to declare the delegate variable outside the loop, create a single delegate instance and reuse it. For example:

    Func<int, int> del = myInstance.MyMethod;
    for (int i = 0; i < 100000; i++)
    {
        MethodTakingFunc(del);
    }
    

    is more efficient than:

    for (int i = 0; i < 100000; i++)
    {
        MethodTakingFunc(myInstance.MyMethod);
    }
    

    Could this have been the problem you were seeing?

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

Sidebar

Ask A Question

Stats

  • Questions 284k
  • Answers 284k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I'd choose Table2. With the Table1 schema you need two… May 13, 2026 at 4:32 pm
  • Editorial Team
    Editorial Team added an answer Use HTTP authentication to log into your Hudson server with… May 13, 2026 at 4:32 pm
  • Editorial Team
    Editorial Team added an answer Your default target is "mac" and you have rule how… May 13, 2026 at 4:32 pm

Related Questions

I am currently running into a problem where an element is coming back from
i have a text file with the following data: Calculated Concentrations 30.55 73.48 298.25
Following this question: Good crash reporting library in c# Is there any library like
Following this question I decided to use ffmpeg to crop MP3s. On another question
Following this question , why does enumerable in this: Type type = typeof(List<string>); bool

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.