473,394 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Interface VS Delegate

When should you use an interface over a delegate?

Like if you have a class that needs to sort various types of objects.
Should it use a delegate to do the sorting or require that the objects
implement a sorting interface?

I read that delegates are smaller and of finer granularity than
interfaces. What does that mean exactly? Does it mean they are more
efficient?

Thanks.
Nov 16 '05 #1
3 8859
Hi there... uhm... there are a couple of things you should be aware of...

Interfaces allow us to extend some object's functionality, it's a contract
between the interface and the object that implements it. It is used to
simulate multiple inheritance in C#. In the other hand, we have delegates...
They're just safe callbacks or function pointers. They allow us to notify
that something has happened (Events). As you can see they're different as
well as their use. In your case I'd recommend you to implement an interface
to the objects you want to sort unless... you want to implement some sort of
quicksort algorithm that has a callback (delegate in .NET) as a parameter.

Regards,
--
Angel J. Hernández M.
MCSD

"Phill" <wa********@yahoo.com> escribió en el mensaje
news:ac**************************@posting.google.c om...
When should you use an interface over a delegate?

Like if you have a class that needs to sort various types of objects.
Should it use a delegate to do the sorting or require that the objects
implement a sorting interface?

I read that delegates are smaller and of finer granularity than
interfaces. What does that mean exactly? Does it mean they are more
efficient?

Thanks.

Nov 16 '05 #2
One way to look at a delegate is that it's an interface with one method.

If your sorting class needs just one method to determine the order of two
objects, then you can use a delegate for that.

Christian

---

"Phill" <wa********@yahoo.com> wrote in message
news:ac**************************@posting.google.c om...
When should you use an interface over a delegate?

Like if you have a class that needs to sort various types of objects.
Should it use a delegate to do the sorting or require that the objects
implement a sorting interface?

I read that delegates are smaller and of finer granularity than
interfaces. What does that mean exactly? Does it mean they are more
efficient?

Thanks.

Nov 16 '05 #3
Christian Heide Damm <ch*****@microsoft.com> wrote:
One way to look at a delegate is that it's an interface with one method.

If your sorting class needs just one method to determine the order of two
objects, then you can use a delegate for that.


Indeed, "interface with one method" is how I often think about it too.
There are a few important differences though:

o Delegates can be multi-cast. This allows events to be subscribed to
by multiple delegates, effectively. (This could be done with a list
of interface implementations, of course.)

o You can specify any appropriate method for a delegate - it doesn't
have to use a specific name, unlike interfaces (in C#, anyway)

o The method can be static

o The method can be private

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: serge calderara | last post by:
Dear all, I have a VB.NET application which is able to identify and load dynamically other dll's. created for my application. Those dll's are working as plugins for my main application. I...
2
by: Derrick | last post by:
How does one declare an event within an interface, so that every class which implements that interface must implement that event? I think I just need to specifiy the actual event as I would a...
3
by: Chua Wen Ching | last post by:
Hi there, I just read Chris Sells's article at http://www.codeproject.com/csharp/delegate_bedtime.asp?df=100&forumid=2983&select=922269#xx922269xx I wonder i can do this: 1) I want to...
2
by: COLIN JACK | last post by:
Hi All, I've got a situation where I'm implementing an interface (BaseInterface in example below) and I want to use explicity interface implementation of an event so that I can add type safety. ...
2
by: mats | last post by:
Hi! This is a quite involved question concerning two dll's that are consumed from a console application. The first dll is called base.dll and is compiled in mixed mode (managed and unmanaged...
4
by: Steven Cummings | last post by:
Hello, I'm currently developing on a large project where several assemblies build into DLLs that support the final .NET EXE. The original plan was to implement the project in VB.NET, so the EXE...
1
by: Anthony Yott | last post by:
Anyone have an example of defining a custom event (i.e custom delegate) in an interface? -- Anthony Yott
5
by: atwomey | last post by:
When I try and place a delegate in an interface, like this : public interface ITest { double foo(); delegate void bar(); } I get an error "delegates cannot declare types". What is it about a...
3
by: pigeonrandle | last post by:
Hi, I have a couple of forms on my client application that use an asyncronous tcp client to send and recieve data from a server application. On each of these forms, i have a Listbox which i would...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.