Understanding Interfaces in C#
page 2 of 5
by Brendan Enrick
Feedback
Average Rating: 
Views (Total / Last 10 Days): 10510/ 401

Creating an Interface

There are times when you will want to define your own interface. When this situation arises you are probably trying to architect software in such a way as to allow change easily later on. Interfaces are a great tool for scaling software projects. When you define an interface it is done similarly to a class. You give it a name and the contents of the interface are between two curly braces. The contents will be methods, properties, events, and indexers. These components are what the interface is requiring be included in any classes implementing the interface.

Listing 1: IHelloWorlder Definition

interface IHelloWorlder
{
  void SayHello();
  void SayGoodBye();
} 

As you can see I have not actually defined any functionality here. All I have done is said what functionality a class using this interface needs to implement. You'll want to note the name I've used here. It is common practice to prefix interface names with an I. Some examples of this practice can be seem with the following interfaces; IDisposable, IDataReader, and IEnumerable. I am sure everyone reading this has used at least one of the interfaces without knowing it. There are plenty of commonly used classes which implement these interfaces.


View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 7 and 1 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/5/2008 1:35:47 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search