Collection interfaces

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

    Collection interfaces

    Confused about this concept. If I define some collection class and inherit
    IList, I get an error if I don't implement the interface methods. This
    makes sense.

    But if I inherit from CollectionBase, I don't get this error, yet the
    methods still don't seem to be implemented. For example, this code gives a
    compile error.

    class TestColl : IList
    {
    }

    because, for one, it doesn't implement Add(...)

    But this one does not give an error, even though the source for
    CollectionBase shows that it does not implement Add(...) either.

    class TestColl : CollectionBase, IList
    {
    }

    What concept am I missing here?


  • Jon Skeet [C# MVP]

    #2
    Re: Collection interfaces

    nooboy <nospam@nospam. comwrote:
    Confused about this concept. If I define some collection class and inherit
    IList, I get an error if I don't implement the interface methods. This
    makes sense.
    >
    But if I inherit from CollectionBase, I don't get this error, yet the
    methods still don't seem to be implemented. For example, this code gives a
    compile error.
    They're implemented by CollectionBase, but explicitly. In other words,
    if you cast a CollectionBase to IList, you can use them.

    --
    Jon Skeet - <skeet@pobox.co m>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    World class .NET training in the UK: http://iterativetraining.co.uk

    Comment

    • nooboy

      #3
      Re: Collection interfaces


      "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
      news:MPG.21d373 d817d65ae721@ms news.microsoft. com...
      nooboy <nospam@nospam. comwrote:
      >Confused about this concept. If I define some collection class and
      >inherit
      >IList, I get an error if I don't implement the interface methods. This
      >makes sense.
      >>
      >But if I inherit from CollectionBase, I don't get this error, yet the
      >methods still don't seem to be implemented. For example, this code gives
      >a
      >compile error.
      >
      They're implemented by CollectionBase, but explicitly. In other words,
      if you cast a CollectionBase to IList, you can use them.
      Did you mean to say "explicitly "? In any case, let's take Add() as an
      example. Where is it actually implemented?


      Comment

      • Liz

        #4
        Re: Collection interfaces


        "nooboy" <nospam@nospam. comwrote in message
        news:OBsCnrmQIH A.4656@TK2MSFTN GP03.phx.gbl...
        Confused about this concept. If I define some collection class and
        inherit IList, I get an error if I don't implement the interface methods.
        This makes sense.
        >
        But if I inherit from CollectionBase, I don't get this error, yet the
        methods still don't seem to be implemented. For example, this code gives
        a compile error.
        >
        class TestColl : IList
        {
        }
        >
        because, for one, it doesn't implement Add(...)
        >
        But this one does not give an error, even though the source for
        CollectionBase shows that it does not implement Add(...) either.
        sure it does ... did you look at the "Explicit Interface Implementations " in
        the docs? you'll see System.Collecti ons.IList.Add

        >
        class TestColl : CollectionBase, IList
        {
        }
        >
        What concept am I missing here?
        >

        Comment

        • nooboy

          #5
          Re: Collection interfaces


          "Liz" <liz@tiredofspa m.comwrote in message
          news:untzmEnQIH A.4712@TK2MSFTN GP04.phx.gbl...
          >
          >>
          >But this one does not give an error, even though the source for
          >CollectionBa se shows that it does not implement Add(...) either.
          >
          sure it does ... did you look at the "Explicit Interface Implementations "
          in the docs? you'll see System.Collecti ons.IList.Add
          I see. Thank you. I assume that's what Jon meant when he said
          "explicitly ". Can you tell me where it is in the source? i.e. when I click
          on CollectionBase in my source code and "Go To Definition", the source code
          doesn't show it. Is there an easy way to get to it?


          Comment

          • Peter Duniho

            #6
            Re: Collection interfaces

            On Wed, 19 Dec 2007 10:41:46 -0800, nooboy <nospam@nospam. comwrote:
            [...]
            >>But if I inherit from CollectionBase, I don't get this error, yet the
            >>methods still don't seem to be implemented. For example, this code
            >>gives
            >>a compile error.
            >>
            >They're implemented by CollectionBase, but explicitly. In other words,
            >if you cast a CollectionBase to IList, you can use them.
            >
            Did you mean to say "explicitly "? In any case, let's take Add() as an
            example. Where is it actually implemented?
            Yes, that's what he meant.

            Read the docs on interfaces in C#. Particularly the difference between an
            implicit and explicit implementation. With the latter, you can only use
            the interface member through the interface itself (e.g. by casting the
            instance to that interface).

            It's implemented by CollectionBase. But because it's an explicit
            implementation, you have to cast your CollectionBase instance to IList
            first before you can use it.

            Pete

            Comment

            • Peter Duniho

              #7
              Re: Collection interfaces

              On Wed, 19 Dec 2007 11:08:13 -0800, nooboy <nospam@nospam. comwrote:
              I see. Thank you. I assume that's what Jon meant when he said
              "explicitly ". Can you tell me where it is in the source? i.e. when I
              click
              on CollectionBase in my source code and "Go To Definition", the source
              code
              doesn't show it. Is there an easy way to get to it?
              CollectionBase is implemented the same place all the other .NET classes
              are. In the .NET framework itself.

              Microsoft has announced they will be making the .NET source code available
              for debugging purposes, but until that's actually happened and you've
              configured your Visual Studio to be able to find the source code, you
              won't be able to see it.

              If you want, for the moment AFAIK you can use Reflector to look at the
              implementation. But Visual Studio won't be able to show it to you.

              Pete

              Comment

              • Liz

                #8
                Re: Collection interfaces


                "nooboy" <nospam@nospam. comwrote in message
                news:O9ffrLnQIH A.280@TK2MSFTNG P03.phx.gbl...
                >
                "Liz" <liz@tiredofspa m.comwrote in message
                news:untzmEnQIH A.4712@TK2MSFTN GP04.phx.gbl...
                >>
                >>>
                >>But this one does not give an error, even though the source for
                >>CollectionBas e shows that it does not implement Add(...) either.
                >>
                >sure it does ... did you look at the "Explicit Interface Implementations "
                >in the docs? you'll see System.Collecti ons.IList.Add
                >
                I see. Thank you. I assume that's what Jon meant when he said
                "explicitly ". Can you tell me where it is in the source? i.e. when I
                click on CollectionBase in my source code and "Go To Definition", the
                source code doesn't show it. Is there an easy way to get to it?

                what Peter says is correct, but consider:

                class LCollection : CollectionBase
                {

                }

                private void myMethod()
                {
                IList l = new LCollection();
                l.Add("some string value");
                }

                if you highlight "Add" and right-click into "Go To Definition" VS *will*
                show you the IList interface definition and comments from the metadata ...
                but not the source code for the Add method

                Notice that IList also combines ICollection and IEnumerable, so all members
                of those interfaces are implemented by implementers of IList.



                Comment

                • nooboy

                  #9
                  Re: Collection interfaces


                  "Liz" <liz@tiredofspa m.comwrote in message
                  news:eGGGccoQIH A.3676@TK2MSFTN GP06.phx.gbl...
                  >
                  >
                  what Peter says is correct, but consider:
                  >
                  class LCollection : CollectionBase
                  {
                  >
                  }
                  >
                  private void myMethod()
                  {
                  IList l = new LCollection();
                  l.Add("some string value");
                  }
                  >
                  if you highlight "Add" and right-click into "Go To Definition" VS *will*
                  show you the IList interface definition and comments from the metadata ...
                  but not the source code for the Add method
                  Right, exactly, that was my question. It's confusing to be taken to source
                  code, but not being able to see the actual implementation. It's a little
                  frustrating because it looks like they're showing you everything, but
                  they're not telling you which parts they're not showing.


                  Comment

                  Working...