What is a Method Group Conversion in C#?

 Posted by Me_Himanshu on 12/27/2013 | Category: C# Interview questions | Views: 15575 | Points: 40
Answer:

Since Version 2.0,C# simplifies the syntax that assigns a method to a delegate.This is called Method Group Conversion .
It allows you to simply assign the name of the method to a delegate without using a new operator or explicitly invoking the delegate's constructor.
For E.g
Suppose we have a method name ReplaceSpace(string str)
Now i have a delegate del in the same class then we assign a method to a delegate with the simple syntax-:
del d=new del(ReplaceSpace);
string temp="";
//and now i will call the method
temp=d("Hello this is an example);

Now with method group conversion i will not use the new operator.
del d=ReplaceSpace ;
string temp="";
temp=d("Hello this is an example)

where del is my delegate defined earlier.

and in case my method is in another class then
class cs=new class();
del d=cs.ReplaceSpace//


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response

More Interview Questions by Me_Himanshu