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

  • Thread starter Thread starter S_K
  • Start date Start date
S

S_K

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
 
S_K said:
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

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
 
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

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!!!
 
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

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
 
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)
 
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
 
Back
Top