Delegates

G

Guest

I’m just starting to learn delegates. I’m at the very beginning. If I understand correctly, delegates are for when you want to pass a function as a parameter. For example the client provides a custom function to be called in a provider class. My confusion is that you can already achieve this by interfaces and objects without delegates (which have a little more complicated syntax)

Here is an example without delegates. class Class1 and class MyProc are on the client side and class Special is on the provider side. In short what delegates have more
class Class

static void Main(string[] args

Special s = new Special()
MyProc mp = new MyProc()

s.PrintValues(mp)



class MyProc : Isa

public int proc (int a

return (a * 2)



interface Isa

int proc(int a)


class Special

private int a = 10

public void PrintValues(Isam o

Console.WriteLine("a processed value is " + o.proc(a))

}
 
J

Justin Rogers

The different between delegates and interfaces is the visibility. Interfaces
require that all implementations be public and that they match by name.
Delegates on the other hand have no name resolution (I can cast any matching
function to the delegate type), and they can have any visibility.

If you really want to breathe some similarity between the two then I'd say that
delegates are lightweight interfaces in that they let you specify the binding
semantics at run-time (I match function to delegate at run-time rather than at
compile time with interfaces), the delegate stores all information about the
target (aka, I don't need an object or interface reference), some delegates can
be combined (aka multi-cast, for multi-function per call).


--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers


Sam said:
I'm just starting to learn delegates. I'm at the very beginning. If I
understand correctly, delegates are for when you want to pass a function as a
parameter. For example the client provides a custom function to be called in a
provider class. My confusion is that you can already achieve this by interfaces
and objects without delegates (which have a little more complicated syntax).
Here is an example without delegates. class Class1 and class MyProc are on the
client side and class Special is on the provider side. In short what delegates
have more?
class Class1
{
static void Main(string[] args)
{
Special s = new Special();
MyProc mp = new MyProc();

s.PrintValues(mp);
}
}

class MyProc : Isam
{
public int proc (int a)
{
return (a * 2);
}
}

interface Isam
{
int proc(int a);
}

class Special
{
private int a = 10;

public void PrintValues(Isam o)
{
Console.WriteLine("a processed value is " + o.proc(a));
}
}
 
R

Ramiro Calderón

Hi,

Although it is true that you can achieve the same functionality without
delegates, it is pretty straightforward and by far much cleaner (syntactic
sugar ;) ). But from a language

1. The delegates supports multicasting, which means that you can invoke more
than one method when the delegate is invoked. In fact, from the event caller
perspective, you can traverse the invocation list before execute one of such
methods.

2. Events are based on delegates, which are widely used in the .NET
framework itself (winforms, async pattern, and such)


Regards,


Sam said:
I'm just starting to learn delegates. I'm at the very beginning. If I
understand correctly, delegates are for when you want to pass a function as
a parameter. For example the client provides a custom function to be called
in a provider class. My confusion is that you can already achieve this by
interfaces and objects without delegates (which have a little more
complicated syntax).
Here is an example without delegates. class Class1 and class MyProc are on
the client side and class Special is on the provider side. In short what
delegates have more?
class Class1
{
static void Main(string[] args)
{
Special s = new Special();
MyProc mp = new MyProc();

s.PrintValues(mp);
}
}

class MyProc : Isam
{
public int proc (int a)
{
return (a * 2);
}
}

interface Isam
{
int proc(int a);
}

class Special
{
private int a = 10;

public void PrintValues(Isam o)
{
Console.WriteLine("a processed value is " + o.proc(a));
}
}
 
M

Mark Broadbent

just to add to point 1. ( for Sam's benefit)

You can modify (add and remove methods to and from) the invocation list so
that the delegate is a dynamic structure (as opposed to a static one)

--


Br,
Mark Broadbent
mcdba , mcse+i
=============
Ramiro Calderón said:
Hi,

Although it is true that you can achieve the same functionality without
delegates, it is pretty straightforward and by far much cleaner (syntactic
sugar ;) ). But from a language

1. The delegates supports multicasting, which means that you can invoke more
than one method when the delegate is invoked. In fact, from the event caller
perspective, you can traverse the invocation list before execute one of such
methods.

2. Events are based on delegates, which are widely used in the .NET
framework itself (winforms, async pattern, and such)


Regards,


Sam said:
I'm just starting to learn delegates. I'm at the very beginning. If I
understand correctly, delegates are for when you want to pass a function as
a parameter. For example the client provides a custom function to be called
in a provider class. My confusion is that you can already achieve this by
interfaces and objects without delegates (which have a little more
complicated syntax).
Here is an example without delegates. class Class1 and class MyProc are
on
the client side and class Special is on the provider side. In short what
delegates have more?
class Class1
{
static void Main(string[] args)
{
Special s = new Special();
MyProc mp = new MyProc();

s.PrintValues(mp);
}
}

class MyProc : Isam
{
public int proc (int a)
{
return (a * 2);
}
}

interface Isam
{
int proc(int a);
}

class Special
{
private int a = 10;

public void PrintValues(Isam o)
{
Console.WriteLine("a processed value is " + o.proc(a));
}
}
 

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