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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • S_K

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

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

    #2
    Re: C# question... What are INTERFACES used for?



    They are the beginning of a long journey on the road of OO (Object Oriented)
    Programming.

    ...




    "S_K" <steve_kershaw@ yahoo.comwrote in message
    news:757d2999-9101-4f03-9510-d3bd06d5e8ba@z1 6g2000prn.googl egroups.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

    Comment

    • Lloyd Sheen

      #3
      Re: C# question... What are INTERFACES used for?


      "S_K" <steve_kershaw@ yahoo.comwrote in message
      news:757d2999-9101-4f03-9510-d3bd06d5e8ba@z1 6g2000prn.googl egroups.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

      Comment

      • S_K

        #4
        Re: C# question... What are INTERFACES used for?

        On May 9, 9:57 am, "Lloyd Sheen" <a...@b.cwrot e:
        "S_K" <steve_kers...@ yahoo.comwrote in message
        >
        news:757d2999-9101-4f03-9510-d3bd06d5e8ba@z1 6g2000prn.googl egroups.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!!!

        Comment

        • Lloyd Sheen

          #5
          Re: C# question... What are INTERFACES used for?


          "S_K" <steve_kershaw@ yahoo.comwrote in message
          news:5edc19e3-a5cb-4a4e-81c3-7cc4ee505102@n1 g2000prb.google groups.com...
          On May 9, 9:57 am, "Lloyd Sheen" <a...@b.cwrot e:
          "S_K" <steve_kers...@ yahoo.comwrote in message
          >
          news:757d2999-9101-4f03-9510-d3bd06d5e8ba@z1 6g2000prn.googl egroups.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

          Comment

          • sloan

            #6
            Re: C# question... What are INTERFACES used for?


            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" <sloan@ipass.ne twrote in message
            news:e4vH1jesIH A.3616@TK2MSFTN GP06.phx.gbl...

            >
            They are the beginning of a long journey on the road of OO (Object
            Oriented) Programming.
            >
            ..
            >
            >
            >
            >
            "S_K" <steve_kershaw@ yahoo.comwrote in message
            news:757d2999-9101-4f03-9510-d3bd06d5e8ba@z1 6g2000prn.googl egroups.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
            >
            >

            Comment

            • =?Utf-8?B?YnJ1Y2UgYmFya2Vy?=

              #7
              RE: C# question... What are INTERFACES used for?

              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
              >

              Comment

              Working...