473,796 Members | 2,599 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
Jun 27 '08 #1
6 1887
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.

...


"S_K" <st***********@ yahoo.comwrote in message
news:75******** *************** ***********@z16 g2000prn.google groups.com...
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

Jun 27 '08 #2

"S_K" <st***********@ yahoo.comwrote in message
news:75******** *************** ***********@z16 g2000prn.google groups.com...
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 myTalkingAnimal s as new List(of IMakeASound)

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

for each animal in myTalkingAnimal s
animal.Talk()
next

This is just a start.

LS

Jun 27 '08 #3
S_K
On May 9, 9:57*am, "Lloyd Sheen" <a...@b.cwrot e:
"S_K" <steve_kers...@ yahoo.comwrote in message

news:75******** *************** ***********@z16 g2000prn.google groups.com...
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 myTalkingAnimal s as new List(of IMakeASound)

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

for each animal in myTalkingAnimal s
* * 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<IMakeASoun danimal = new List<IMakeASoun d>();

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!!!
Jun 27 '08 #4

"S_K" <st***********@ yahoo.comwrote in message
news:5e******** *************** ***********@n1g 2000prb.googleg roups.com...
On May 9, 9:57 am, "Lloyd Sheen" <a...@b.cwrot e:
"S_K" <steve_kers...@ yahoo.comwrote in message

news:75******** *************** ***********@z16 g2000prn.google groups.com...
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 myTalkingAnimal s as new List(of IMakeASound)

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

for each animal in myTalkingAnimal s
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<IMakeASoun danimal = new List<IMakeASoun d>();

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

Jun 27 '08 #5

Take a peek here:

http://sholliday.space s.live.com/Blog/cns!A68482B9628 A842A!126.entry
it'll show you (one) way to use Interfaces.

...

"sloan" <sl***@ipass.ne twrote in message
news:e4******** ******@TK2MSFTN GP06.phx.gbl...
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.

..


"S_K" <st***********@ yahoo.comwrote in message
news:75******** *************** ***********@z16 g2000prn.google groups.com...
>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


Jun 27 '08 #6
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)
"S_K" wrote:
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
Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

44
4289
by: lester | last post by:
a pre-beginner's question: what is the pros and cons of .net, compared to ++ I am wondering what can I get if I continue to learn C# after I have learned C --> C++ --> C# ?? I think there must be many know the answer here. thanks
4
7298
by: emma middlebrook | last post by:
Hi Straight to the point - I don't understand why System.Array derives from IList (given the methods/properties actually on IList). When designing an interface you specify a contract. Deriving from an interface and only implementing some of it means something is wrong: either the interface specification is wrong e.g. not minimal or the derivation is wrong e.g. the type can't actually honour this contract.
7
1341
by: Fred Mellender | last post by:
I would like to make a library (dll) that contains a number of classes and interfaces for use by other implementers. One of the interfaces I want to define is: public interface Foo { Bar MakeABar(); }
13
3265
by: John Salerno | last post by:
Hi all. I have a question about interfaces now. According to the book I'm reading, when you implement an interface, the class or structure has to declare all the methods that the interface includes. My question is, if you must declare each method (as opposed to simply 'using' those methods like an #included file), then why not just declare the methods on your own as if you created them yourself? What advantage does using an interface...
7
1225
by: tshad | last post by:
I am trying to understand why I would use interfaces. In the following example for IPrinciple, I have the following code: ************************************************************ using System; using System.Collections; using System.Security; using System.Security.Principal;
7
4476
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears to be checking that the correct data type is used DataAcess sets an abstract class and methods
22
2116
by: RSH | last post by:
Hi, I have been reading on interfaces working on samples I've run across on the web. For the life of me I cannot seem to grasp them. It appears to me that interfaces are simply blueprints to a class, that when implemented, they require the implementing calss to make sure that each of the properties, functions etc. are handled by the class. (????) What I am really having a problem with is how they overcome the limitation
27
3863
by: jm | last post by:
I am having trouble understanding the purposes of an interface, even though the concept of interfaces is around me all the time (user interface, for example). I'm just not understanding software interfaces. Like anything else, it appears Interfaces are by design something that requires documentation. I know this may be obvious, but if I have a class A and I say, well, class A implements IMyInterface. The fact that it implements...
23
2225
by: A.Gallus | last post by:
If I declare a function pure virtual: class A { virtual void myfunc() = 0; } and I derive a class from A: class B : public A
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10449
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10217
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10168
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5568
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.