Help me to understand better the interfaces

Hi

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


Please help!!!



Answer this question

Help me to understand better the interfaces

  • SeRya

    ok.. i think i am understanding..

    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

    You want to use a compiler Can you explain a little more I don't understand why you need to make some compiler interface.

  • 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

    zapacila89 wrote:

    anyway.. that are the use of interfaces

    I don't understand question, what do you mean



  • jslx7

    so.. an interface.. is something that all should have ..

    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

    Sergey Galich wrote:

    Hi!

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


  • MURALEE KRISHNAN

    lets .. take an example.. if i make a server class .. ok

    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:

    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.



  • chickplee

    Hi!

    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.


  • 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

    When you have a Ball interface:


    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

    Try this:

    http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpgenref/html/cpconbaseclassusageguidelines.asp


  • Mike Waldron

    forget that..

    anyway.. that are the use of interfaces


  • Help me to understand better the interfaces