i read a bunch of articles on articles.. i know exactly how to amke them.. But i dont understand why should i use them .. or for what are they important..
Why should someone wanna make a interface for something...
public class BasketBall : IBall { public double GetSize() { return 1.5d; } }
public class FootBall : IBall { public double PrintName() { return 2.0d; } }
You can create an IBall instance of any type the implements this interface. We print the size of the ball without knowing what the type of the ball is. If you add a new ball type to your project, you don't have to break all your code. Only implement the IBall interface and you are ready to go.
IBall ball = new BasketBall(); Console.WriteLine( ball.GetSize() );
ball = new FootBall(); Console.WriteLine( ball.GetSize() );
Interface used to implement behavior in any class without specific superclass.
Few examples:
IList. Any class that implement this interface can be treated like list.
ya but i have to define all the methods that has that interface.. how will i know what methods its needs.. and what is i dont know what to write to that methods..
Interface used to implement behavior in any class without specific superclass.
Few examples:
IList. Any class that implement this interface can be treated like list. This can be .NET Framework class or your own. And there is no need to make them all from the same base class.
IFormattable. Any object that implement this interface can be used with formatting services.
IDisposable interface used to mark any object so it can be used in USING statement and can be disposed manually.
IButtonControl. Any control that implement button behavior can implement IButtonControl. So designer can enumerate all controls and find controls that can be treated as buttons, so you can select AcceptButton property in Form.
If you implement interface - you must read docs first and use smart tag to implement methods/properties stubs.
If you design own interface - you must find minimal set of methods/properties that will implement required behavior.
You will need interfaces in library or extensible projects. When you create some code that can work with objects, but you don't know this objects types.
Try imagine how you will implement IEnumerable interface without interface Your goal is to be able enumerate object's content. Without interface you will create base class EnumerableBase and declare virtual methods there. All classes must be derived from EnumerableBase to be able be enumerated. Now imagine same thing about ICloneable, IFormattable, IComparable... So many base classes How to create new classes from multiple parents And can you provide default implementation of IEnumerable No. This is abstraction only, each class will have own details.
It's not correct to say "inherit from interface" (except complex case, like inheriting interface from another interface). Interface is implemented. You inherit from class. Sample with balls typically implemented like this:
public abstract class Ball {
public abstract float Radius { get; }
}
public class FootBall : Ball {
public override float Radius { get { return 10.0; } }
}
public class BaseBall : Ball {
public override float Radius { get { return 3.0; } }
}
Interface sometimes reffered as contract between two components.
For nothing. You need reasons. Server-side component is not the reason to create interfaces.
You will create interfaces in systems that are extensible or aggregates. Do you create such big systems
You must know how to use them, what standard interfaces used for - this will help you write programs. When you encounter big, extensible projects - then you will understand that this is time for interfaces.
For example, you want to create system that can work with multiple payment systems, then you declare interface of the abstract payment system and each payment system can implement this interface, so your product can use them.
PJ, I think "create an IBall instance of any type" can confuse people, you can't create interface instance. I think you mean "create instance of any class and cast it to IBall"
Anyway, we need better example. This one do not show difference between base class and interfaces. But I don't see some good idea right now, this must be some kind of service, that can interact with user-provided components and interfaces must be suitable for it.
Help me to understand better the interfaces
bforst
public interface IBall
{
double GetSize();
}
public class BasketBall : IBall
{
public double GetSize()
{
return 1.5d;
}
}
public class FootBall : IBall
{
public double PrintName()
{
return 2.0d;
}
}
You can create an IBall instance of any type the implements this interface.
We print the size of the ball without knowing what the type of the ball is. If you add a new ball type to your project, you don't have to break all your code. Only implement the IBall interface and you are ready to go.
IBall ball = new BasketBall();
Console.WriteLine( ball.GetSize() );
ball = new FootBall();
Console.WriteLine( ball.GetSize() );
monztre
for example.. PJ example:
if i want to create a new ball object.. a baseball ball .. i have to inherts from IBall.. and then i can really say that i have a ball
an interface is something.. like universal ball..
Jeff Schindler
ya but i have to define all the methods that has that interface.. how will i know what methods its needs.. and what is i dont know what to write to that methods..
Mark Watkin
but.. this days.. i wanet to use .. a complier. c# provideed by .Net.. and some one said that i have to make an interface of the compiler.. why
vishwas_udy
and i also make an interface for it.. 4 what will this interface be usefull for someone
Simeao
I don't understand question, what do you mean
Exellon
tachikoma
Hi!
Interface used to implement behavior in any class without specific superclass.
Few examples:
50322899
If you implement interface - you must read docs first and use smart tag to implement methods/properties stubs.
If you design own interface - you must find minimal set of methods/properties that will implement required behavior.
You will need interfaces in library or extensible projects. When you create some code that can work with objects, but you don't know this objects types.
Try imagine how you will implement IEnumerable interface without interface Your goal is to be able enumerate object's content. Without interface you will create base class EnumerableBase and declare virtual methods there. All classes must be derived from EnumerableBase to be able be enumerated. Now imagine same thing about ICloneable, IFormattable, IComparable... So many base classes How to create new classes from multiple parents And can you provide default implementation of IEnumerable No. This is abstraction only, each class will have own details.
epoc
It's not correct to say "inherit from interface" (except complex case, like inheriting interface from another interface). Interface is implemented. You inherit from class. Sample with balls typically implemented like this:
Interface sometimes reffered as contract between two components.
Sudheer_sgk
For nothing. You need reasons. Server-side component is not the reason to create interfaces.
You will create interfaces in systems that are extensible or aggregates. Do you create such big systems
You must know how to use them, what standard interfaces used for - this will help you write programs. When you encounter big, extensible projects - then you will understand that this is time for interfaces.
For example, you want to create system that can work with multiple payment systems, then you declare interface of the abstract payment system and each payment system can implement this interface, so your product can use them.
Geze
anyway.. that are the use of interfaces
bhayes
http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpgenref/html/cpconbaseclassusageguidelines.asp
Manojk.kothari
PJ, I think "create an IBall instance of any type" can confuse people, you can't create interface instance. I think you mean "create instance of any class and cast it to IBall"
Anyway, we need better example. This one do not show difference between base class and interfaces. But I don't see some good idea right now, this must be some kind of service, that can interact with user-provided components and interfaces must be suitable for it.