You are on page 1of 5

c   

m? What is a Delegate?
A delegate acts like a strongly type function pointer. Delegates can invoke the
methods that they reference without making explicit calls to those methods.

m? Where are Delegates used?


The most common example of using delegates is in events.

You define a method that contains code for performing various tasks when an event
(such as a mouse click) takes place. This method needs to be invoked by the runtime
when the event occurs. Hence this method, that you defined, is passed as a parameter
to a delegate.

m? Delegates are reference type variables , which holds the reference of the method.

m? That is we can decide the execution of method at run time based on our requirements.

m? ven though it is used to call method at runtime its primary use in c# is used for
implementing events and call-back methods.

m? A delegate in C# is similar to a function pointer in C or C++.

m? A function pointer is a variable that stores the address of a function that can later be
called through that function pointer

m? delegates in c# are very similar to function pointers in c++ the only difference is that
delegates are type safe due to these are CLR targeted under .net framework, while
function pointers in c++ are not type safe.?Simply say Delegate is a strongly typed
function pointer

m? A Delegate in c# allows to pass a method of class to object of other class.

Π 

m? c   ? ?


 ?  ? ?  ??
m? c   ? ?
 ?  ? ?  ??
m? c   ?  
?   ? ?  ? ? ?  
?? ??
~   
      

  ?
?
?
?
c  
??? ? 
?  ?
?
?? ?  ? 
?
?
 ?
c  
? ?    ? ?
??  
 ??  ? ??
  ?
 ??
??  
? 
?
?? ? ??? ? ? 
?  
? ?
 
 ??   ???
???

??  
?? 

?? 
?? ?



? ? ?

??  ?Ñ ?? 

?~? 

???????   ??




?!?
? ??

? ?  ?? 

???
?
" ??c  
? ? ?  
???
 
??

#? ? ??   ??   ?


?
 ? 
?  ? ?
 ?? ? ?
? ???  
 ???   ?"? ? ? 
 ??
  ? ?
Ñ ?
?
 ?
? ??

2.? Public void print()


3.? {
4.? Console.Writeline("Hello")
5.? }

Î? ?
 ?? ?   ? 
?  ? ?
 ??  ? ??

Public void Mydelegate();

$ ? ? ??   ??


?? ? ???
? ?
?
 ?% 
?
&? ? ? ? ?  ?   ?  ? ? ?
 ?? ? 
??
?
?   ?  ?
?? ?? ?
??

public string MySecondDelegate(int number);

   ?   ? ?  


 ?
 ?'  ?
? ?
?
 ?  
?
  ?  ?
?
 ??

(? ? 
?
 ?  ?? ?  
?  ? 
? ?
 ?
 ?
? ?   ?
  ??
)? ? 
?
 ?   ? ?   ? ??

? ?  ?
??  ??
   ??

X.? Mydelegate l_objMyDelegate=new Mydelegate(print);

 ?  ??  
 ??

*? Î? ? ?  


 ?  ?
?   ? ??
?

?
     ?
?

Ñ

    ?
?

?
 
 ?
?

?
Œ  Ñ   Ñ  ?
?

?

  Ñ   Ñ  ?
?

    ?
?
 ??!?+  ,??

private void btnPentiumI_Click(object sender, System.EventArgs e)


{
Computer l_objComputer=new Computer();
ComputerType l_objCallComputer=new
ComputerType(l_objComputer.PentiumI);
l_objCallComputer();
}
 ??!?+  ,,??

private void btnPentiumIISS_Click(object sender, System.EventArgs e)


{
Computer l_objComputer=new Computer();
ComputerType l_objCallComputer=new
ComputerType(l_objComputer.PentiumII);
l_objCallComputer();
}
?
?
?? ?? ?  ??? ? ??  ?   ?? ?? 

?  ??


 ??
?   ?? ?? ?  ? ?
?  % ?? ?  
 ??


? ?
 ? ?
?
?  ? ??
?  ? ?   ?  ?? ?
-  ? ? ?  ??
?
 ??   ? 

??

using System;
using System.Windows.Forms;

namespace delegatesprac
{
///
/// Summary description for Computer.
///
public class Computer
{
///
/// Method petium! that has the similar
/// signature as Delegate ComputerType
///
public void PentiumI()
{
MessageBox.Show("Configuration: Pentium! 200MMX "+
"2.1 GB HardDrive "+
"32 MB RAM "+
"32X CD-ROM");
}

///
///Method petium!! that has the similar
/// signature as Delegate ComputerType
///
public void PentiumII()
{
MessageBox.Show("Configuration: Pentium!! 350MMX "+
"4.3 GB HardDrive "+
"64 MB RAM "+
"40X CD-ROM");
}
}
///
/// Delegate Computer type
///
public delegate void ComputerType();

}
c
??
?
% ? ? ? 
?? 

? ?? 
??  
?+  ,??+  ,,?
? ?
 ? ??  ? ?   ? ?
?   ?
? 
?
 ?
 ?
?
 ? ?Ñ  ??Ñ  ?Î? ? ??
?Ñ  ??
Ñ  ??? ?? ?  ??? ? ??  ?   ?? ?? 

?
  ?? ??
?   ?? ?? ?  ? ?
?  % ?? ?
 
 ??

? ?
 ? ?
?
?  ? ??
?  ? ?   ?
  ?? ? -  ? ? ?