Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Interfaces

Interfaces

Scheduled Pinned Locked Moved C#
csharpquestion
12 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J jan larsen

    Interfaces is a method to provide polymorphism without having to implement multiple inheritance. An interface is a description of a subset of properties and methods of an object. If an object is declared to implement an interface, then it must provide all the properties and methods of that interface. A good example of the usefullness of interfaces is IDisposable. Let's say that you got a list of objects. The objects are all of different types, but you know that some of those objects needs to release their internal resources when you're done using them. Here is how you could do it:

    foreach (IDisposable disposableObject in listOfVariousClassInstances)
    {
    disposableObject.Dispose();
    }

    In the example above, you don't care if the object at hand is a Brush, BinaryWriter or a Cursor, you simply narrows the object to an IDisposable and invokes the one and only method available: Dispose(). When to create an interface: If you expect that your class hierarchy contains classes that share common methods and properties, you will of course use inheritance:

    public class Base
    {
    protected int i;

    public int I
    {
    get:
    {
    return i;
    }
    }
    }

    public class A: Base
    {
    protected int j;

    public int J
    {
    get:
    {
    return j;
    }
    }
    }

    public class B: Base
    {
    protected int k;

    public int K
    {
    get:
    {
    return k;
    }
    }
    }

    But suppose that you want class B to be a list of objects too. All that functionality could be easily provided by extending class System.Collections.ArrayList, but then you can't inherit from class Base. You know that other classes in your hierarchy will refer to B.I, so you now have the choice to either reinvent the functionality of ArrayList, or create a common interface for your classes. In this case, it seems that your classes aren't that related, which in itself begs for an interface solution, but it's quite clear, that it should be easier to copy/paste the I property to all of your classes, than to reinvent ArrayList:

    public interface IBase
    {
    int I
    {
    get;
    }
    }

    public class A: IBase
    {
    protected int j;
    protected int i;

    public int I
    {
    get:
    {
    return i;
    }
    }

    public int J
    {
    get:
    {
    return j;
    }
    }
    }

    public class B: ArrayList, IBase
    {
    protected int k;
    protected int i;

    public int I
    {

    B Offline
    B Offline
    Bahadir Cambel
    wrote on last edited by
    #3

    Thanks a lot for the examples and the explanations.

    1 Reply Last reply
    0
    • J jan larsen

      Interfaces is a method to provide polymorphism without having to implement multiple inheritance. An interface is a description of a subset of properties and methods of an object. If an object is declared to implement an interface, then it must provide all the properties and methods of that interface. A good example of the usefullness of interfaces is IDisposable. Let's say that you got a list of objects. The objects are all of different types, but you know that some of those objects needs to release their internal resources when you're done using them. Here is how you could do it:

      foreach (IDisposable disposableObject in listOfVariousClassInstances)
      {
      disposableObject.Dispose();
      }

      In the example above, you don't care if the object at hand is a Brush, BinaryWriter or a Cursor, you simply narrows the object to an IDisposable and invokes the one and only method available: Dispose(). When to create an interface: If you expect that your class hierarchy contains classes that share common methods and properties, you will of course use inheritance:

      public class Base
      {
      protected int i;

      public int I
      {
      get:
      {
      return i;
      }
      }
      }

      public class A: Base
      {
      protected int j;

      public int J
      {
      get:
      {
      return j;
      }
      }
      }

      public class B: Base
      {
      protected int k;

      public int K
      {
      get:
      {
      return k;
      }
      }
      }

      But suppose that you want class B to be a list of objects too. All that functionality could be easily provided by extending class System.Collections.ArrayList, but then you can't inherit from class Base. You know that other classes in your hierarchy will refer to B.I, so you now have the choice to either reinvent the functionality of ArrayList, or create a common interface for your classes. In this case, it seems that your classes aren't that related, which in itself begs for an interface solution, but it's quite clear, that it should be easier to copy/paste the I property to all of your classes, than to reinvent ArrayList:

      public interface IBase
      {
      int I
      {
      get;
      }
      }

      public class A: IBase
      {
      protected int j;
      protected int i;

      public int I
      {
      get:
      {
      return i;
      }
      }

      public int J
      {
      get:
      {
      return j;
      }
      }
      }

      public class B: ArrayList, IBase
      {
      protected int k;
      protected int i;

      public int I
      {

      D Offline
      D Offline
      DavidNohejl
      wrote on last edited by
      #4

      jan larsen wrote: foreach (IDisposable disposableObject in listOfVariousClassInstances) { disposableObject.Dispose(); } That won't work if at least one item from listOfVariousClassInstances will NOT implement IDisposable, right? I guess you know it, bt I think that rookie may not know... so I say it :) foreach loop does type casting for every item in collection, so it will raise exception if some item from listOfVariousClassInstances collection can't by casted to IDisposable. correct me if I am wrong... As I do :) best regards, David 'DNH' Nohejl Never forget: "Stay kul and happy" (I.A.)

      B J 2 Replies Last reply
      0
      • D DavidNohejl

        jan larsen wrote: foreach (IDisposable disposableObject in listOfVariousClassInstances) { disposableObject.Dispose(); } That won't work if at least one item from listOfVariousClassInstances will NOT implement IDisposable, right? I guess you know it, bt I think that rookie may not know... so I say it :) foreach loop does type casting for every item in collection, so it will raise exception if some item from listOfVariousClassInstances collection can't by casted to IDisposable. correct me if I am wrong... As I do :) best regards, David 'DNH' Nohejl Never forget: "Stay kul and happy" (I.A.)

        B Offline
        B Offline
        Bahadir Cambel
        wrote on last edited by
        #5

        well "that rookie" knows about it... and I didnt like the kind of the way you pointing me.. I'm sorry, but you should be more polite...:~

        D 1 Reply Last reply
        0
        • B Bahadir Cambel

          well "that rookie" knows about it... and I didnt like the kind of the way you pointing me.. I'm sorry, but you should be more polite...:~

          D Offline
          D Offline
          DavidNohejl
          wrote on last edited by
          #6

          Bahadir Cambel wrote: well "that rookie" knows about it... OK. Don't call me only ignorant* but pesimistic ignorant from now. My theory is better say something obvious again than forget to say something critical. Bahadir Cambel wrote: I'm sorry, but you should be more polite.. you mean "that rookie" ? Sorry than.. I am so stupid I cannot remember who was author of original post as I see only post I am replying to... so I had to use something else than your name... well i could use "s/he" ... *click* another lesson learnt At the top of the things, I always had problems (nope, I never... others have problem wit me ) to be polite... and I am not going to change myself in this way much... sorry man that's the way I am. David *that's another, long story Never forget: "Stay kul and happy" (I.A.)

          B 1 Reply Last reply
          0
          • D DavidNohejl

            Bahadir Cambel wrote: well "that rookie" knows about it... OK. Don't call me only ignorant* but pesimistic ignorant from now. My theory is better say something obvious again than forget to say something critical. Bahadir Cambel wrote: I'm sorry, but you should be more polite.. you mean "that rookie" ? Sorry than.. I am so stupid I cannot remember who was author of original post as I see only post I am replying to... so I had to use something else than your name... well i could use "s/he" ... *click* another lesson learnt At the top of the things, I always had problems (nope, I never... others have problem wit me ) to be polite... and I am not going to change myself in this way much... sorry man that's the way I am. David *that's another, long story Never forget: "Stay kul and happy" (I.A.)

            B Offline
            B Offline
            Bahadir Cambel
            wrote on last edited by
            #7

            ok , the critical thing is not critical for me right now, and when I consider the example , it enlightened me a lot..So I didnt intend to write or answer about the IDispose.. I wont talk about about the politeness , thats your choice , and if you like the way ppl threaten to you , than everything is fine.. Whatever, I know your are trying to help ppl , the way you do..it is ok for me... Bahadir

            D 1 Reply Last reply
            0
            • B Bahadir Cambel

              ok , the critical thing is not critical for me right now, and when I consider the example , it enlightened me a lot..So I didnt intend to write or answer about the IDispose.. I wont talk about about the politeness , thats your choice , and if you like the way ppl threaten to you , than everything is fine.. Whatever, I know your are trying to help ppl , the way you do..it is ok for me... Bahadir

              D Offline
              D Offline
              DavidNohejl
              wrote on last edited by
              #8

              well, one more try... was it that "rookie" thing? :confused: my problem can be that I DONT see anything rude in that post :confused: Bahadir Cambel wrote: and if you like the way ppl threaten to you actually I don't. Most people don't like when i say what I think.. bt saying something else is something I detest. David Never forget: "Stay kul and happy" (I.A.)

              1 Reply Last reply
              0
              • D DavidNohejl

                jan larsen wrote: foreach (IDisposable disposableObject in listOfVariousClassInstances) { disposableObject.Dispose(); } That won't work if at least one item from listOfVariousClassInstances will NOT implement IDisposable, right? I guess you know it, bt I think that rookie may not know... so I say it :) foreach loop does type casting for every item in collection, so it will raise exception if some item from listOfVariousClassInstances collection can't by casted to IDisposable. correct me if I am wrong... As I do :) best regards, David 'DNH' Nohejl Never forget: "Stay kul and happy" (I.A.)

                J Offline
                J Offline
                jan larsen
                wrote on last edited by
                #9

                Ah, yes, there is that. But all that will be corrected when we get generics... "After all it's just text at the end of the day. - Colin Davies "For example, when a VB programmer comes to my house, they may say 'does your pool need cleaning, sir ?' " - Christian Graus

                D 1 Reply Last reply
                0
                • J jan larsen

                  Ah, yes, there is that. But all that will be corrected when we get generics... "After all it's just text at the end of the day. - Colin Davies "For example, when a VB programmer comes to my house, they may say 'does your pool need cleaning, sir ?' " - Christian Graus

                  D Offline
                  D Offline
                  DavidNohejl
                  wrote on last edited by
                  #10

                  true. Did you find my post unpolite? was "rookie" bad? :~ David Never forget: "Stay kul and happy" (I.A.)

                  J 1 Reply Last reply
                  0
                  • D DavidNohejl

                    true. Did you find my post unpolite? was "rookie" bad? :~ David Never forget: "Stay kul and happy" (I.A.)

                    J Offline
                    J Offline
                    jan larsen
                    wrote on last edited by
                    #11

                    Well, if he truly was a rookie, it wasn't bad. But he could be a very good C programmer that is about to learn OOP, in that case, he would probably find rookie a bit insulting :-) I was a consultant in a .NET project a couple of years ago (a very bold decision from a big company to use the brand new technology), where more than half of the staff were new to OOP, and they certainly weren't at home with GC. I used a lot of hours reeding out resource leaks by inserting dispose() invocations and encapsulating functionality inside using(...) blocks. I even had to expose my ears to statements like: "Hey, look what Bob did!, isn't it clever?, now we can use this code in blah blah blah", the guy was talking about 'bob' having implemented a class for Gods sake! But I never considered them rookies, at many points, some of them were much more experienced programmers, than I was. "After all it's just text at the end of the day. - Colin Davies "For example, when a VB programmer comes to my house, they may say 'does your pool need cleaning, sir ?' " - Christian Graus

                    D 1 Reply Last reply
                    0
                    • J jan larsen

                      Well, if he truly was a rookie, it wasn't bad. But he could be a very good C programmer that is about to learn OOP, in that case, he would probably find rookie a bit insulting :-) I was a consultant in a .NET project a couple of years ago (a very bold decision from a big company to use the brand new technology), where more than half of the staff were new to OOP, and they certainly weren't at home with GC. I used a lot of hours reeding out resource leaks by inserting dispose() invocations and encapsulating functionality inside using(...) blocks. I even had to expose my ears to statements like: "Hey, look what Bob did!, isn't it clever?, now we can use this code in blah blah blah", the guy was talking about 'bob' having implemented a class for Gods sake! But I never considered them rookies, at many points, some of them were much more experienced programmers, than I was. "After all it's just text at the end of the day. - Colin Davies "For example, when a VB programmer comes to my house, they may say 'does your pool need cleaning, sir ?' " - Christian Graus

                      D Offline
                      D Offline
                      DavidNohejl
                      wrote on last edited by
                      #12

                      jan larsen wrote: But he could be a very good C programmer that is about to learn OOP, in that case, he would probably find rookie a bit insulting Oh yeah... :-O I was thinking in contex of what he is learing... bt no one can know what I am thinking unless I write it down :doh: - my fault. I understand now... thx David Never forget: "Stay kul and happy" (I.A.)

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups