   Delegates
  The only time I've used something like delegates before is in the SICP workshop in Scheme. So I'm trying to build my view of delegates off of lambda functions in Scheme, and those two clearly aren't toe-to-toe in all matters. So here's a couple of questions:1) Are delegates created at runtime? (if so, how?)2) Is there much of an overhead for delegates?3) Can you think of any other differences between delegates and lambda functions that I would need to know?And while we're on the subject of Scheme, does C# work well with recursion (does tail recursion work?), or does the stack overflow the same way as C++?   
  1) A delegate is created partly at runtime and partly at compile time. When an entity is bound to a delegate a new instance of the Delegate class is created; this happens at runtime. The Delegate object then contains member variables that contain a reference to the object bound to the delegate (if any) and a reference to the function being bound. The code for the delegate is created at compile time, both for the object that encapsulates the delegates behavior and the code that binds the object to the delegate.2) Using delegates costs about as much as any equivalent functionality would cost. It's more expensive than a straight up virtual function call on a known function on a known object, so if you know both of those up front then don't use a delegate.3) Off the top of my head... in Scheme all functions are implemented in terms of lambda expressions, in C# delegates are implemented in terms of functions. In Scheme there's no compile time type checking, so you can use duck typed lambdas. In C# delegates are delegates are statically typed. As far as I can tell, currently C# is not properly tail-recursive in the same sense that Scheme is properly tail-recursive. This may change in future updates to the compiler or .NET runtime. But it can definitely cause a stack overflow from recursive calls.   ;  Thanks for the answers! I'm still struggling a bit with question number one, but let's see if I'm correct here (unfortunately, I don't have a C# compiler at the moment - my main computer is having issues):class DelegateTester{  delegate void DelegateType( int a );  DelegateType d;    int b;  public DelegateTester()  {    b = 5;    d = delegate( int a )    { return a + b;    }  }  public void Test()  {    d( 5 ); // This would return 10    b = 0;    d( 5 ): // Would this return 10 or 0?  }}If I understand you correctly, when  I initialize d, it is bound to the instance of DelegateTester. But is the value of b inserted determined at the time of creation of the delegate, or at the time of running the delegate? What if b had been a reference type?   ;  It would return 5. In this case the anonymous delegate is bound to the member variable of DelegateTester; this shouldn't change if it's a value type or a reference type. However, if you instead wrote DelegateTester() like so:    public DelegateTester() { int b = 5; d = delegate(int a) {   return a + b; };    }It would return 10. In this case the anonymous delegate would be implemented as an anonymous class that contains a member variable initialized to the value of b when it's bound. This value in turn, would stay set, no matter what happened to the DelegateTester() that it contained. (Well aside from getting garbage collected when the DelegateTester went out of scope.)   ;  Great! Makes sense now.   