473,802 Members | 1,971 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

returning interfaces

In some of the code I am looking at on a sample website, a lot of
methods are returning interfaces such as IDataReader instead of a plain
DataReader. Why would you want to do this?

*** Sent via Developersdex http://www.developersdex.com ***
Jun 27 '08 #1
3 1448
Mike P wrote:
In some of the code I am looking at on a sample website, a lot of
methods are returning interfaces such as IDataReader instead of a plain
DataReader. Why would you want to do this?

*** Sent via Developersdex http://www.developersdex.com ***
Because then the implementation can return an object that looks like a
IDataReader and behaves the way it is supposed to, without actually
having to inherit from DataReader.

Interfaces are useful for making your own classes in your own class
hierarchy pluggable into some other kind of class hierarchy which has
nothing in common with yours.

--
Lasse Vågsæther Karlsen
mailto:la***@vk arlsen.no
http://presentationmode.blogspot.com/
PGP KeyID: 0xBCDEA2E3
Jun 27 '08 #2
On Apr 17, 6:56*am, Mike P <mike.p...@gmai l.comwrote:
In some of the code I am looking at on a sample website, a lot of
methods are returning interfaces such as IDataReader instead of a plain
DataReader. *Why would you want to do this?

*** Sent via Developersdexht tp://www.developersd ex.com***
Hi,

Because you do not care about the actual type being returned, you do
not care if it's a SqlDataReader or an OracleDataReade r, the only
thing you care about is that it implements the IDataReader interface.
Jun 27 '08 #3
It separates the interface from the implementation.

Here is a decent explanation....

http://bytes.com/forum/thread223924.html

-Drew
"Ignacio Machin ( .NET/ C# MVP )" <ig************ @gmail.comwrote in
message
news:09******** *************** ***********@24g 2000hsh.googleg roups.com...
On Apr 17, 6:56 am, Mike P <mike.p...@gmai l.comwrote:
In some of the code I am looking at on a sample website, a lot of
methods are returning interfaces such as IDataReader instead of a plain
DataReader. Why would you want to do this?

*** Sent via Developersdexht tp://www.developersd ex.com***
Hi,

Because you do not care about the actual type being returned, you do
not care if it's a SqlDataReader or an OracleDataReade r, the only
thing you care about is that it implements the IDataReader interface.

Jun 27 '08 #4

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

Similar topics

1
2213
by: baylor | last post by:
In C#, an interface cannot mark any method as static. i'm told the ILASM supports it but i've never tested that Two questions. First, why? OK, i've heard the reason about interfaces being allocated on the stack but that's not a real reason. Which is OK because this isn't a real question, it's just a complaint dressed up to look like a reason Second question. If a language doesn't support a fairly obvious feature, one has to wonder if...
41
3832
by: Materialised | last post by:
I am writing a simple function to initialise 3 variables to pesudo random numbers. I have a function which is as follows int randomise( int x, int y, intz) { srand((unsigned)time(NULL)); x = rand(); y = rand();
30
2518
by: Frank Rizzo | last post by:
We are having one of those religious debates at work: Interfaces vs Classes. My take is that Classes give you more flexibility. You can enforce a contract on the descendant classes by marking methods abstract (which is all that an interface does). In addition, the classes allow you to be flexible by adding functionality that child classes inherit. Or by providing optional functionality via virtual methods. Now, I understand that...
8
1786
by: John | last post by:
What is the purpose / benefit of using an interface statement? It doesn't seem like anything more than a different way to make a class... (except you can't define any procedures in an interface like you can do in a class). I'm obviously missing something... John
5
10378
by: LS | last post by:
Can a WebMethod return an Interface type? Can we pass an interface parameter ? Example : public interface IEntity { long Id { get; set; } string Name { get; set; } }
18
1989
by: _dee | last post by:
Question about best use of interfaces: Say there's a 'Master' class that needs to implement a few interfaces: class Master : I1, I2, I3 { } The actual code already exists in smaller classes that each
22
2116
by: RSH | last post by:
Hi, I have been reading on interfaces working on samples I've run across on the web. For the life of me I cannot seem to grasp them. It appears to me that interfaces are simply blueprints to a class, that when implemented, they require the implementing calss to make sure that each of the properties, functions etc. are handled by the class. (????) What I am really having a problem with is how they overcome the limitation
17
2105
by: Cralis | last post by:
I am trying to populate a ListView with a list of 'Models' of cars. I have a data object class for my models, which has a function, 'getListOfModels', which I want to retuyrn a <Listof models. In my data object, I have also created a class to hold each model, as shown below. No database work has been done. Just adding a model, and trying to return it.
5
1834
by: DamienS | last post by:
Hi, I have a static method in a class and I need to be able to return a reference to "this". Googling around, I found a heap of discussions of the pros/cons of "abstract static" etc. It was quite a heated debate about purity of OO design that just did my head in a bit. In a nutshell. Can a static method 'know' what class it's defined in and return that type information?
0
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10538
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10285
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10063
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9115
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3792
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.