Why we should use delegate?

S

Swapnil

As we know we can call function from the class by using object of the class
where that function is present.
Then what is new in delegate it also calls the function but the difference
is that we are providing wrapper around the function call. iI know it is
typesafe. Explain with some strong cocept(reason).
Then why we should use Delegate?
e.g.
Suppose i have function in Myclass class

class Myclass{ void Add (int a){}}

To call this function i will use object of Myclass.

Myclass objMyclass=new Myclass();
objMyclass.Add(4);

like this i can call the function.

But by using delegate
delegate void Delegatename(int x); //definition of dlegate
Delegatename objDelegate=new Delegatename(objMyclass.Add(4));

so by using delegate we r giving overheads then why we should use delegate?
 
M

Mufaka

You wouldn't use it like this in practice. Delegates provide a level of
indirection where your instance can be invoked by a class that has no
knowledge of your method beyond it being an instance of a delegate type.
 
J

jehugaleahsa

As we know we can call function from the class by using object of the class
where that function is present.
 Then what is new in delegate it also calls the function but the difference
is that we are providing wrapper around the function call. iI know it is
typesafe. Explain with some strong cocept(reason).
 Then why we should use Delegate?
e.g.
   Suppose i have function in Myclass class

class Myclass{ void Add (int a){}}

To call this function i will use object of Myclass.

Myclass objMyclass=new Myclass();
objMyclass.Add(4);

like this i can call the function.

But by using delegate
delegate void Delegatename(int x);     //definition of dlegate
Delegatename objDelegate=new Delegatename(objMyclass.Add(4));

so by using delegate we r giving overheads then why we should use delegate?

In my experience, delegates come in handy when you are working with
collections. Outside of collections, delegates can be used, as said,
to add a level of indirection. Additionally, multicast delegates allow
multiple methods to be attached at once - most people see this through
events.

Keeping that in mind, delegates should NEVER be used where a good
class heirarchy and/or interfaces and/or polymorphic behavior makes
sense. Like I said, my primary use for delegates have been to work on
a collection, where the delegates play the role of black boxes, the
changing part of the code. If you think about that, delegates provide
a way of working on data where the data may not adhere to a specific
interface. The behavior might change but the data will not.
Polymorphic behavior can work across a heirarchy, but not within the
same class.

So, in summary, delegates find there greatest use, for me, in
1) Events
2) Generic Algorithms and Data Structures
3) The rare need to change a class' behavior at runtime.

Fortunately, most delegates are built-in and you don't have much to
do. If delegates don't make sense, you're trying to hard to use them.
Keep them in your toolset and an opportunity will show itself.

~Travis Parks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top