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

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
public void DoSomething()
    MyHelloWorld helloWorld = new MyHelloWorld();

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

User Comments

Title: Interface   
Name: karthik
Date: 2013-01-16 3:23:31 AM
Title: Interface   
Name: Garv
Date: 2012-12-29 1:32:17 AM
Very Right explanation.As a beginner i am expecting a usage of interfaces in window form apllications, web form etc.

Lets say i have to make a customer form so how should i use interface in a proper way

Garv Singh Rana
Title: Interface   
Name: Bhanu
Date: 2012-12-14 9:09:17 AM
yeah...your given description is very effective for a beginners...good going keep it up...
Title: Surajit   
Name: Surajit
Date: 2012-11-28 6:39:25 AM
Title: Anil   
Name: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Date: 2012-11-28 4:44:10 AM
Title: rft   
Name: ret
Date: 2012-11-28 4:43:23 AM
Title: Nice article   
Name: Bond
Date: 2012-09-09 2:10:14 AM
can we write examples of interface
Title: RE: Good article but still very less information about function datatypes.   
Name: Brendan Enrick
Date: 2012-07-25 10:10:28 AM
Hi Shital,

Thanks for the question. No, you cannot define access modifiers for interface methods.
Title: Good article but still very less information about function datatypes.   
Name: shital
Date: 2012-07-25 3:56:00 AM
Can we write protected or internal method declaration in interface?
Title: Good definition   
Name: Durkha
Date: 2012-06-15 1:32:48 AM
Very nice explanation of Interface.
Title: Simple and Nice to understand for any one   
Name: suresh
Date: 2011-11-26 1:06:36 AM
Nice means of getting understand what interfae is for beignners
Title: Very well explained   
Name: Michael Harper
Date: 2011-08-09 4:20:13 PM
I've been struggling with the concept of interfaces even after reading a chapter in my C# book that covers them in depth. This article got through to me what they are actually used for and how they can make applications "Plug and play" so to speak!

Title: simplified   
Name: Kuldip
Date: 2011-05-11 9:18:14 AM
Excellent Article.
Very Good explanation of interface
Title: Good Defination   
Name: Sarwan
Date: 2011-03-02 11:19:55 AM
Its really helpful for me.... and once again thanks to you.
Title: Good Definition   
Name: Santosh Joshi
Date: 2011-02-15 7:38:04 AM
This helped a lot.
Title: good answer   
Name: javed khan
Date: 2011-01-21 3:53:52 AM
thanks u for its.
Title: fantastic explanation   
Name: Paul P
Date: 2011-01-19 3:44:05 PM
Thanks - I've been agonising over interfaces.... Now finally the penny has dropped and the light at the end of the tunnel is definately back on.

Thanks again - outstanding explanation.
Title: Interfaces   
Name: Manohar
Date: 2010-12-21 2:48:35 AM
Title: Good fundamental material   
Name: SK Srivastava
Date: 2010-12-06 8:11:01 AM
Fundamentals of interface would be clear.
Title: Nice Article   
Name: David
Date: 2010-09-30 4:34:35 AM
Nice Article. What are the advantages of interface.
Title: Good start   
Name: Kiran
Date: 2010-09-22 7:27:32 AM
nice article.
Title: Comprehensible   
Name: gets the beginner onboard
Date: 2010-09-13 1:24:21 AM
You have to go over it a few times, but it becomes clearer each time you do. Other articles seem clear at first, some even tell you "it's very simple"..but the more you go over them the worse the comprehension. This article excludes everything unnecessary but does not make you deduce anything in order to comprehend. I hate it when I'm learning and have to use stop and use deductive energy before I have gained comprehension. Kudos
Title: Excellent Article   
Name: Excellent
Date: 2010-08-19 8:52:17 PM
Excellent writer who writes smooth and easy to follow article. Thanks
Title: Infterface   
Name: Very Good
Date: 2010-08-19 10:18:07 AM
Very Good explanation of interface.Smooth English and
good working program codes.
Title: Very Nice   
Name: Gajendra Paliwal
Date: 2010-07-22 2:39:16 AM
Thanks ,such a good explanation
Title: Very Good Explanation   
Name: Sam
Date: 2010-07-19 2:39:53 PM
thanks allot for such a good explanation - never read on any site - keep posting good articles like this one - but need simple as well as some complex examples also.
Title: Interfaces   
Name: Ana
Date: 2010-04-28 3:07:06 PM
Thank you so much for taking your time in posting this excellent article.
Title: Good explanations   
Name: James B Nelson
Date: 2010-04-21 5:28:42 AM
Very nice article. I was beginning to understand what an interface was from various google searches, but had no idea why I would care about them until your "using interfaces" section, which is missing from most discussions. It seems to be a very powerful tool that I would have overlooked or dismissed without your article!
Title: Interface   
Name: Prashanth
Date: 2010-03-29 5:58:06 AM
Great Article.Its thhe besi have ever seen.Very very helpful for beginners.
Title: Interface   
Name: Bipin
Date: 2010-03-12 8:32:05 AM
Great Article.I am very confuse in Interface and Abstract class but when i read this article then my confusion is clear.
Title: interfaces   
Name: roberto
Date: 2010-02-11 9:14:36 PM
You lost me with "Using Interfaces". This appears to be a great way to confuse a developer. c# is a nightmare.
Title: Great Article   
Name: Duff
Date: 2010-02-09 10:40:10 AM
Great Article! Very easy to follow and understand the reason for using Interfaces. Great Work! Keep it up!
Title: Very Precise and easily apprehended   
Name: BSM
Date: 2010-01-09 1:26:57 AM
I have understood the interface concept very easily with this article. Good work
Title: Vey good article   
Name: KCH
Date: 2010-01-03 3:20:51 PM
Title: Excellant   
Name: GLT
Date: 2010-01-02 9:11:05 PM
Best definition of an interface I have ever seen!!!
Title: Great start...   
Name: David
Date: 2009-11-24 4:11:17 PM
My only complaint - it's too short. A great start, though and I've already learned enough from the article to search for more advanced materials...
Title: Greatness   
Name: joe
Date: 2009-08-26 3:00:21 PM
Thanks for the easy to understand definition as to what Interfaces are good for. I've been reading up on them and no where have I found such a simple explanation of when it is beneficial to use interfaces. Thanks!
Title: Nice Article   
Name: Shashi
Date: 2009-08-21 12:52:12 AM
looks good,very simple and to the point. Excellent for beginners
Title: Great Article......   
Name: Ashwin
Date: 2009-07-31 5:43:23 AM
What more can a beginner like me ask for.......??
Title: Or just maybe...   
Name: mck
Date: 2009-06-20 9:46:32 AM
I think there might be a glimmer of light. I`ll need to do a bit more reading to make sure.
Title: Nope   
Name: mck
Date: 2009-06-19 12:19:15 PM
Still a total blank for me I`m afraid. Got the example up and running and I just can`t see what the point or benefits
of using the interface is. I`ve never come across anything in programming thats has left me totally bewildered as interfaces have. Its a black hole for me.
Title: Great Article   
Name: Srini
Date: 2009-06-06 10:49:08 AM
I've been trying to understand interfaces from many days. This is the best one I came across.

Great Job.

Title: thanku   
Name: Sharique
Date: 2009-06-02 4:12:41 PM
nice article,,
Title: Execellent Article on Interfaces   
Name: Nithya Mohanrajh
Date: 2009-04-27 12:17:02 AM
Nice article on Interfaces.
Made Understanding very easy with simple and neat explanation.Thanks for the Good work & GoodLuck.
Title: Nice   
Name: Kavita
Date: 2009-03-31 2:28:38 AM
Great article. I had read so many article to understand this..but this was the best one.. keep it up.
Title: Thank You   
Date: 2009-03-18 3:16:28 PM
Great article keep up the good work... This has really helped.
Title: Thank You!   
Name: suji
Date: 2009-01-20 5:25:21 AM
Very good article.Easy to grasp in a very short time.
apprecitae you in taking time in presenting such a good article.short and crisp.
Title: thanku   
Name: sania
Date: 2008-10-22 1:21:09 AM
Very good article.Easy to grasp in a very short time.
apprecitae you in taking time in presenting such a good article.short and crisp.
Title: thanks:aspalliance   
Name: senthil kumar
Date: 2008-08-29 5:36:41 AM
i understand the use of interface by aspalliance interface.
Title: Can add more to interface   
Name: Imtiaz Mohammed
Date: 2008-07-31 8:46:00 AM
Thanks for the information on Interface.It would be great if some more example with more complexity can be added.As the usage of Interface is high while designing classes and interfaces,add more info...Overall it was good knowledge piece.
Title: Thank You!   
Name: Rich
Date: 2008-07-25 5:02:08 PM
Sweet and Short to give me a high level understanding of Interfaces. It's articles like these that make things clear and are not congested with complex code to make the author feel smart!
Again , Great Job and thanks for clearing things up on this subject.
Title: Great effort.   
Name: Atul Kumar
Date: 2008-06-13 10:14:40 PM
Excellent clarification of the concept.
Title: Feedback   
Name: Brendan Enrick
Date: 2008-05-28 10:00:02 AM
@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: Just reviewing   
Name: Dale
Date: 2008-05-21 1:54:23 PM
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: 2008-04-10 6:28:50 AM
No at all great but some what ok
Title: interface   
Name: bala
Date: 2008-03-19 6:50:33 AM
No at all great but some what ok
Title: Interfaces   
Name: santosh kumar patro
Date: 2008-03-16 11:08:27 AM
Thnx very much sir for your help regarding Interfaces.
Title: this.title.new   
Name: this.user.next
Date: 2008-02-29 3:51:43 AM
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: 2008-02-22 3:38:03 AM
Excellent article..keep up the good work.
Title: Great job   
Name: George
Date: 2008-02-18 12:57:35 AM
its a great interface document
a guy like me know now how to use interfaces
Title: interface   
Name: chaitu
Date: 2008-01-22 4:23:52 AM
it is very help full
Title: Very good explanation, please provide more like this !!!   
Name: Stefan
Date: 2008-01-16 3:47:00 AM
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: 2008-01-02 2:06:51 AM
It is very help full.
Title: very good article   
Name: Mohsen
Date: 2007-12-28 4:38:40 PM
yes you've made it very easy understandable ,, thank you
Title: Very informative   
Name: Farrukh
Date: 2007-12-22 2:25:23 AM
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.

Title: Interface   
Name: Arvind Gupta
Date: 2007-12-21 10:14:14 AM
Very UseFull !
Title: Static Methods   
Name: Brendan
Date: 2007-12-13 9:50:33 AM
You can read some more about this on my blog post where I talk about this and go int more details about the topic.

Title: Static Methods   
Name: Brendan
Date: 2007-12-13 8:55:20 AM

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,
Title: what about static functions?   
Name: Jan
Date: 2007-12-13 7:32:28 AM
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: 2007-12-11 10:27:56 AM
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: 2007-12-07 12:53:23 PM
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: 2007-12-07 9:05:25 AM
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: 2007-12-07 12:31:08 AM
Helpful for naive C# programmers. Should have taken a better example.

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-05-22 6:56:36 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search