24

Possible Duplicate:
are there function pointers in c#?

I'm interested in finding the difference between delegate in C# and function pointer in C++.

Community
  • 1
  • 1
Riporter
  • 360
  • 1
  • 4
  • 9

2 Answers2

35

A delegate in C# is a type-safe function pointer with a built in iterator.

It's guaranteed to point to a valid function with the specified signature (unlike C where pointers can be cast to point to who knows what). It also supports the concept of iterating through multiple bound functions.

In C#, delegates are multi-cast meaning they can iterate through multiple functions. For example:

class Program
{
   delegate void Foo();

   static void Main(string[] args)
   {
      Foo myDelegate = One;
      myDelegate += Two;

      myDelegate(); // Will call One then Two
   }

   static void One()
   {
      Console.WriteLine("In one..");
   }

   static void Two()
   {
      Console.WriteLine("In two..");
   }
}
duffymo
  • 305,152
  • 44
  • 369
  • 561
Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
  • Hmmm...I'd say yes and no. You can specify the return type and parameters for a function pointer, which does many of the things you mentioned. The only exception I can think of is if you cast it to a void*. – riwalk Nov 11 '11 at 16:57
  • Yea I guess it's more of a language/runtime difference. All managed pointers are "safe", the runtime protects what they point to and you can't just go increment them or re-interpret them however you wish. Sure, there's "unsafe" code in C# but that's another topic) – Mike Christensen Nov 11 '11 at 17:01
  • Modified my answer slightly to make it more clear.. – Mike Christensen Nov 11 '11 at 17:05
  • `supports the concept of iterating through multiple bound functions.` -- What does that mean? You can't do that with C++? – Robert Harvey Nov 12 '11 at 17:54
  • I don't think you're right about the iterator aspect. It is true that a delegate is a type-safe function, but it doesn't contain an iterator out of the box; although you can use one with an iterator. Can you explain what you mean by that in more detail? http://en.wikipedia.org/wiki/Iterator#C.23_and_other_.NET_languages – Robert Harvey Nov 12 '11 at 17:57
  • 2
    .NET has unicast and multicast delegates. Multicast delegates implement the `+=` and '-=` operators to add and remove methods from the invocation list. All delegates you create in C# are multicast delegates. – Mike Christensen Nov 12 '11 at 19:29
  • @Mike: Right, "Multicast Delegates" is the correct terminology. You can talk about "iterating" through the subscribed functions and calling them, but that's really obscure, and it doesn't have much relevance to the average C# developer, since the manner in which the functions are called is an internal implementation detail (I'm not even sure it uses the usual iterator mechanism, although that seems plausible). – Robert Harvey Nov 12 '11 at 19:33
  • Lemme post an example to make it a bit more clear.. I meant the `delegate` keyword in C# is multicast (this may or may not be the case in other .NET languages). Sure, you can implement multicast delegates in C++ - You can implement anything you want :) – Mike Christensen Nov 12 '11 at 19:52
  • Behavior/usage if the delegate/functions return values: http://stackoverflow.com/q/12530767/1995714 – cp.engr Sep 14 '16 at 15:58
7

Delegates in C# can be either synchronous or asynchronous; C++ function pointers are synchronous unless you write your own multi-threading capability.

A pointer in C/C++ needn't refer to a full-blown object. C had function pointers and no object-oriented language support. Delegates are true function objects.

duffymo
  • 305,152
  • 44
  • 369
  • 561