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...
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.
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.
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..
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.
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.
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.
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() );
Help me to understand better the interfaces
SeRya
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
Jean-Luc David
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.
Toni Dolce
Baq
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.
vad23
I don't understand question, what do you mean
jslx7
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..
John Daldry
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..
MURALEE KRISHNAN
and i also make an interface for it.. 4 what will this interface be usefull for someone
jinksk
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.
chickplee
Hi!
Interface used to implement behavior in any class without specific superclass.
Few examples:
Sakellariou Dimitris
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.
Pravin Kumaradhas
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() );
Sagacity
http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpgenref/html/cpconbaseclassusageguidelines.asp
Mike Waldron
anyway.. that are the use of interfaces