Uses for anonymous methods

I’m trying to better understand the real world use of anonymous methods. I know how to use them, I’m just trying to better understand WHY I would.

I’ve read a lot about them, trying to answer this question for myself, and one of the primary reasons people tend to mention is that they’re marginally faster to execute than direct function calls. So if that’s true, why wouldn’t I ALWAYS use anonymous methods? Most documentation and tutorials say that you should only use them for very short functions, but why? If they’re faster, then who cares about the length? Is it just an issue of readability? I’ll admit that anonymous methods are bit harder to read at a glance, but I’ll get over it if they’re faster. If I’m storing the anonymous method in a delegate then I can call it just like any named method, I can add it as a listener to another delegate, etc. so I’m not sacrificing any usability as far as I can see.

The one place I can definitely see a benefit in anonymous methods is in the ability to declare the anonymous method INSIDE the function call. So in that case I don’t even have a delegate variable, I just pass the entire function body. Speaking of readability, that’s pretty hard to read, but I can certainly see the benefit in that.

Am I missing something?

You got some things wrong here.

First of all why do you think anonymous methods should be faster than any other method? That’s simply not true. An anonymous method is just exactly the same as a normal method, just that it doesn’t have an explicit name. Under the hood the compiler actually creates an actual method (with an internal name).

When passing a lambda expression or an anonymous method to another method as parameter you don’t pass the method body. You actually pass a delegate reference to the method.

Anonymous methods can represent a closure. So you have to be careful how you use them because you can easily create a lot of garbage due to the closure context that is created every time.

Anonymous methods are just “standalone” method. They are usually created in an internal private class that the compiler generates. “Normal” methods of a class can be virtual and can be overridden in derived classes. You can’t do this with anonymous methods

How and where you use anonymous methods is up to you. It’s just a tool. Though in most cases anonymous methods are only used for short linq selectors / predicates where the closure ability is the actual advantage.

Finally i just want to point out this is called an anonymous method:

DoSomething(delegate(string s) { /* your methos body here */ });

while this is a lambda expression:

DoSomething((s)=>{ /* your methos body here */ });

Lambda expressions are similar to anonymous methods but have some advantages:

  • They have a shorter syntax
  • Depending on the usage the compiler will infer the parameter and return type from the usage if possible

If “DoSomething” expects a delegate that takes a string parameter, those 4 examples are the same:

DoSomething((string s)=>{ Debug.Log(s); });
DoSomething(delegate(string s) { Debug.Log(s); });