Understanding Interfaces in C#
page 4 of 5
by Brendan Enrick
Feedback
Average Rating: 
Views (Total / Last 10 Days): 10487/ 417

Using Interfaces

Now that we have an interface implemented we can take a look at how we can actually use this interface. One nice thing we can do now when writing a method which will just be calling our interface's methods is that we can have it take the interface instead of the class. Observe the following example.

Listing 4: Using an Interface as a Parameter

public void GreetEveryone(IHelloWorlder hw)
{
    // Perform whatever logic you wish here
    hw.SayHello();
}
 
public void DoSomething()
{
    MyHelloWorld helloWorld = new MyHelloWorld();
    GreetEveryone(helloWorld);
}

If we were to write another class implementing the IHelloWorlder we could alter the DoSomethingMethod and change it to use the new class. Since we just specified the interface in the GreetEveryone method we will not have to change anything in there. A good example of when this type of scaling might occur can be seen with the IDataReader interface.

As an example, pretend that we have a web site and that the web site uses XML files to store all of its data. We use an XmlDataReader to read all of the data from the XML files. Since we have used IDataReader in the program instead of always specifying XmlDataReader everywhere, we can easily change to using a database later. We would be able to change to using a SqlDataReader for example. We will have less code to change because we are using the IDataReader interface and both classes implement it so a lot of the interaction remains the same. If we were to then decide we wanted a different type of database we could easily switch to another IDataReader-implementing class and continue working.


View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 5 and 7 and type the answer here:

User Comments

Title: Great effort.   
Name: Atul Kumar
Date: 6/13/2008 10:14:40 PM
Comment:
Excellent clarification of the concept.
Title: Feedback   
Name: Brendan Enrick
Date: 5/28/2008 10:00:02 AM
Comment:
@DHIVYAA Well, I am sorry you feel that way. I've tried to write an article which would be easy for people to understand and grasp the concept of an interface. Perhaps you could let me know what it is that you do not like, and I could make an attempt at improving the quality of the article.
Title: INTERFACE   
Name: DHIVYAA
Date: 5/28/2008 6:52:42 AM
Comment:
VERY WORST
Title: Just reviewing   
Name: Dale
Date: 5/21/2008 1:54:23 PM
Comment:
I'm trying to swallow all the .Net now that I have finally made the switch. I read, read, and then short articles like you made, turn the light on for me. I can see !
Title: interface   
Name: zahir
Date: 4/10/2008 6:28:50 AM
Comment:
No at all great but some what ok
Title: interface   
Name: bala
Date: 3/19/2008 6:50:33 AM
Comment:
No at all great but some what ok
Title: Interfaces   
Name: santosh kumar patro
Date: 3/16/2008 11:08:27 AM
Comment:
Thnx very much sir for your help regarding Interfaces.
Title: this.title.new   
Name: this.user.next
Date: 2/29/2008 3:51:43 AM
Comment:
Why the need for all those small parts? It all could have been one whole part.. it irritates me to see all those parts and that too so small??
Title: Excellent Article   
Name: Manpreet
Date: 2/22/2008 3:38:03 AM
Comment:
Excellent article..keep up the good work.
Title: Great job   
Name: George
Date: 2/18/2008 12:57:35 AM
Comment:
its a great interface document
a guy like me know now how to use interfaces
thanks
Title: interface   
Name: chaitu
Date: 1/22/2008 4:23:52 AM
Comment:
it is very help full
Title: Very good explanation, please provide more like this !!!   
Name: Stefan
Date: 1/16/2008 3:47:00 AM
Comment:
It was really a pleasure for me to read this. Not many people are able to explain it that understandable like you did! And it's quite often the most basic things that are confusing if not understood completely.
To those who criticised: be honest and admit that somtimes there's something like this which enlights your priggish minds ;-)
Title: excellent   
Name: Amit
Date: 1/2/2008 2:06:51 AM
Comment:
It is very help full.
Title: very good article   
Name: Mohsen
Date: 12/28/2007 4:38:40 PM
Comment:
yes you've made it very easy understandable ,, thank you
Title: Very informative   
Name: Farrukh
Date: 12/22/2007 2:25:23 AM
Comment:
Yes, you have defined it in a much better and easy and a friendly way.

I was never clear even about its definition by ready books.

Thanks
Title: Interface   
Name: Arvind Gupta
Date: 12/21/2007 10:14:14 AM
Comment:
Very UseFull !
sr_arvind@yahoo.com
Title: Better than Mine   
Name: Buddy Lindsey
Date: 12/18/2007 9:00:53 PM
Comment:
I think that you did a better job explaining interfaaces for beginners than I did on my blog.

http://www.buddylindsey.com/Blog/post/2007/11/Real-World-Interfaces-in-C.aspx
Title: Static Methods   
Name: Brendan
Date: 12/13/2007 9:50:33 AM
Comment:
You can read some more about this on my blog post where I talk about this and go int more details about the topic.

http://aspadvice.com/blogs/name/archive/2007/12/13/Static-Methods-and-Interfaces-in-C_2300_.aspx
Title: Static Methods   
Name: Brendan
Date: 12/13/2007 8:55:20 AM
Comment:
Jan,

One important thing to understand about static methods is that they don't really need to be tied to classes. Static methods basically just exist for convenience.

An interface is as the name says an interface to something. Think of it like an automobile interface. There are defined ways of working with the car. They have a steering wheel a gas pedal and brakes. An interface defines that and all automobiles will have those, but an interface doesn't define anything about "cars in general" (static) it defines what will be the interface for individual cars (instances).

I hope that makes sense,
Brendan
Title: what about static functions?   
Name: Jan
Date: 12/13/2007 7:32:28 AM
Comment:
All my classes implementing my interface should have a static funtion.

Why can't I have a static function in an interface???
Title: It finally sinks in   
Name: acousticsatellite
Date: 12/11/2007 10:27:56 AM
Comment:
I have read several articles on Interfaces. Finally I understand their usage. The portion on swapping out the DataReader is what made things click.
Title: Refreshingly well written   
Name: foton
Date: 12/7/2007 12:53:23 PM
Comment:
It's nice to read an article in which the author writes clearly and well. Nice job. I hope others will follow your example.
Title: Designed for beginners   
Name: Brendan
Date: 12/7/2007 9:05:25 AM
Comment:
Yes, I designed this article to be for beginners. I'll be writing more advanced articles on this same topic later.
Title: Useful link for naive users   
Name: Uday D
Date: 12/7/2007 12:31:08 AM
Comment:
Helpful for naive C# programmers. Should have taken a better example.

Product Spotlight
Product Spotlight 
Learn More
.NET Tools
asp.net shopping cart
asp.net chart control






Ads Powered by Lake Quincy Media
Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2008 ASPAlliance.com  |  Page Processed at 7/4/2008 5:21:52 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search