Understanding Interfaces in C#
page 1 of 5
Published: 06 Dec 2007
Abstract
Interfaces are a very useful tool built into C#. In this article Brendan explains the basics of how interfaces work and for what they may be used. He explains how to create simple custom interfaces and how to implement them.
by Brendan Enrick
Feedback
Average Rating: 
Views (Total / Last 10 Days): 8210/ 383

Introduction

What Interfaces Are

Interfaces basically define a blueprint for a class or a struct. The programmed definition of an interface looks very similar to a class, but nothing is implemented. Interfaces define the properties, methods, events, and indexers, but the interface does not define the implementation of any of these. It just declares their existence. Interfaces will not actually define any functionality. They just define ways in which interactions with a class takes place.

What Interfaces Are Not

Interfaces should not be confused with inheritance. They are two very different things. Inheritance will define a lot of the implementation and is used for code reuse. Interfaces are merely a definition for how communication with the implementing classes must take place. It is like a written contract. A class "signing" the contract will agree to perform certain specified actions in any way it wishes, but it must perform the specified actions.

When to Use Interfaces

Interfaces allow us to create nice layouts for what a class is going to implement. Because of the guarantee the interface gives us, when many components use the same interface it allows us to easily interchange one component for another which is using the same interface. Dynamic programs begin to form easily from this.


View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 4 and 2 and type the answer here:

User Comments

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 






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


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