Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / May 2008

Tip: Looking for answers? Try searching our database.

C# question... What are INTERFACES used for?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
S_K - 09 May 2008 16:21 GMT
Hi,
I've been toying around with interfaces in C#.
They are fun but can anybody give me some examples of where interfaces
are used
and what they are used for?

Thanks so much.
Steve
sloan - 09 May 2008 16:29 GMT
http://www.google.com/search?hl=en&q=OO+Interfaces

They are the beginning of a long journey on the road of OO (Object Oriented)
Programming.

..

> Hi,
> I've been toying around with interfaces in C#.
[quoted text clipped - 4 lines]
> Thanks so much.
> Steve
sloan - 09 May 2008 18:53 GMT
Take a peek here:

http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!126.entry

it'll show you (one) way to use Interfaces.

..

> http://www.google.com/search?hl=en&q=OO+Interfaces
>
[quoted text clipped - 11 lines]
>> Thanks so much.
>> Steve
Lloyd Sheen - 09 May 2008 16:57 GMT
> Hi,
> I've been toying around with interfaces in C#.
[quoted text clipped - 4 lines]
> Thanks so much.
> Steve

The easiest way to understand interfaces is to think of them as a contract.
When an object implements an interface it "contracts" to implement the
methods etc of the interface.

This means that if you have several object which all implement a certain
interface you can use the interface as the variable type.

Following is VB but same in C#.

Lets say I have an object class animal.  I do not want to have talking
animals (make a sound) and non talking animals but I will implement the
IMakeASound interface for animals that make a sound.  This is important
since C# and VB.Net do not allow for multiple inheritance.  Interfaces allow
for an object to overcome this.

I would then be able to have:
interface IMakeASound
   sub Talk()
end interface

dim myTalkingAnimals as new List(of IMakeASound)

I could then add talking animals to this list and do the following:

for each animal in myTalkingAnimals
   animal.Talk()
next

This is just a start.

LS
S_K - 09 May 2008 17:35 GMT
> > Hi,
> > I've been toying around with interfaces in C#.
[quoted text clipped - 36 lines]
>
> LS

So each class implements the same interface eg:

public class Dog: IMakeASound
{
  Talk()
  { return "bow wow"}
}
public class Cat: IMakeASound
{
  Talk()
  { return "meow"}
}

Then you can use this interface in a seperate class:

     List<IMakeASound> animal = new List<IMakeASound>();

     animal.Add(new new Cat());
     animal.Add(new Dog());

     string talk1 = animal[0].Talk();
     string talk2 = animal[1].Talk();

talk1 is "meow"
talk2 is "bow wow"

THAT IS SOOOO COOL!

Thanks so much!!!
Lloyd Sheen - 09 May 2008 18:17 GMT
On May 9, 9:57 am, "Lloyd Sheen" <a...@b.c> wrote:
> "S_K" <steve_kers...@yahoo.com> wrote in message
>
[quoted text clipped - 42 lines]
>
> LS

So each class implements the same interface eg:

public class Dog: IMakeASound
{
  Talk()
  { return "bow wow"}
}
public class Cat: IMakeASound
{
  Talk()
  { return "meow"}
}

Then you can use this interface in a seperate class:

     List<IMakeASound> animal = new List<IMakeASound>();

     animal.Add(new new Cat());
     animal.Add(new Dog());

     string talk1 = animal[0].Talk();
     string talk2 = animal[1].Talk();

talk1 is "meow"
talk2 is "bow wow"

THAT IS SOOOO COOL!

Thanks so much!!!

Where it gets really cool is in thing like the data classes.  There are a
bunch of interfaces that each data class implement.  This means that you can
code a system lets say using Access and very quickly (if you only use the
interfaces) code the same project with SQL Server.  Check out things like
IDDbCommand, IDataAdapter etc.

LS
bruce barker - 09 May 2008 19:57 GMT
with strongly typed langaues like c# and java that don't implement multiple
inheritance (have more than 1 parent class), interfaces are required.

say you wanted to create a new business object collection class that
inherited from ArrayList (collection) and your base business object methods.

in python, you just inherit from both classes and your done.

in ruby (which does not support multiple inheritance) you just create
business methods with the same name and parameters. in either case, you could
pass the collection to code that knew how to call the business object and it
would work. not in c#, it would throw a type error.

in c# your business object defines an interface, and another class
implements it. then that object can be cast to the interface and the methods
called.

-- bruce (sqlwork.com)

> Hi,
> I've been toying around with interfaces in C#.
[quoted text clipped - 4 lines]
> Thanks so much.
> Steve
Cramer - 10 May 2008 02:01 GMT
Your question has a virtually unlimited number of [possible accurate
answers]. It's kind of like asking, "Hey, I just discovered this tool called
a hammer ... what can I use it for?"

A book that deals directly with your question, is Programming .NET
Components by Juval Lowy. Just chapters 1 and 3 are all you really need in
order to gain a clear understanding and  appreciation of Interfaces and
their pervasive role in modern object-oriented programming.

One of the best books, by far, that can help you really "get" some of the
many powerful uses of interfaces Head First Design Patterns by Eric Freeman
and Elizabeth Freeman.

-HTH

> Hi,
> I've been toying around with interfaces in C#.
[quoted text clipped - 4 lines]
> Thanks so much.
> Steve

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.