1

Possible Duplicate:
C#: Difference between ‘ += anEvent’ and ‘ += new EventHandler(anEvent)’

Let us have this delegate :

delegate int Process (int x ,int y) ; 

and this method :

int Add (int x , int y)
{
    return x+y ; 
}

My queston :

what is the difference between :

Process MyProcess = Add ; 

and :

Process MyProcess = new Process (Add) ; 
Community
  • 1
  • 1
Farah_online
  • 561
  • 1
  • 9
  • 20

1 Answers1

6

In C# 1.x only the second version would compile.

C# 2.0 added implicit method group conversions which allows you to write the first version. The two are equivalent. Sometimes it is necessary to use the more explicit form in the case that there is ambiguity.

See the section Delegate inference from Jon Skeet's article Delegate changes for more information.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • 1
    I think I'd call it method group conversions rather than delegate inference these days, admittedly :) Some time I must update that article... – Jon Skeet Aug 05 '10 at 19:24
  • @Jon Skeet: I checked and "method group conversions" is also what it is called in the What's New in C# 2.0 (http://msdn.microsoft.com/en-us/library/7cz8t42e%28VS.80%29.aspx). Thanks for the correction. I hope that your article is updated soon! – Mark Byers Aug 05 '10 at 19:30
  • @Jon Skeet : thanks Mr.Jon for your benefit topic :) , when I read it , I know what is the benefit of "event " (add/remove) but when I declare (add/remove) I can't call the event , in this case the event will be able to appear just on the left hand side of += or -= , How can I rais my event in this case ?? – Farah_online Aug 06 '10 at 09:12
  • @Farah_online: You don't call the event - you use the underlying delegate field which you're using to keep track of the handlers added and removed. – Jon Skeet Aug 06 '10 at 09:16