CollectionBase

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

    CollectionBase

    Hello!

    Assume I have this classes shown below.
    Now to my question List below is inherited from CollectionBase and is a
    property and return an IList according to the documentation.
    I can't figure out if I do "Type myType1 = List.GetType(); " in the add
    method and check myType1 it shows name of Animals
    I thought it should show IList because List is of Type IList.

    But when I do Type myType2= InnerList.GetTy pe();
    then myType2 is an ArrayList which is correct.

    class Animals : CollectionBase
    {
    public void Add(Animal newAnimal)
    {
    List.Add(newAni mal)

    Type myType1= List.GetType();

    Type myType2= InnerList.GetTy pe();


    }
    }


    class Animal
    {
    something here
    }

    //Tony


  • Jon Skeet [C# MVP]

    #2
    Re: CollectionBase

    On Jun 10, 12:39 pm, "Tony" <johansson.ande rs...@telia.com wrote:
    Assume I have this classes shown below.
    Now to my question List below is inherited from CollectionBase and is a
    property and return an IList according to the documentation.
    I can't figure out if I do "Type myType1 = List.GetType(); " in the add
    method and check myType1 it shows name of Animals
    I thought it should show IList because List is of Type IList.
    The *variable* List may be of type IList, but the actual object that
    the variable refers to can't be - you can't have objects which are
    "just" instances of an interface.

    List.GetType() finds out the actual type of the object that List
    refers to.

    Jon

    Comment

    • Marc Gravell

      #3
      Re: CollectionBase

      A minor point, but it is clear that this and the prior posts are all
      using .NET 1.1 features.

      This will work, but at some point you may want to look at generics
      (.NET 2.0); this almost always reduces code complexity etc (although
      there is obviously an initial "what are generics" step...).

      Or if you want to finish whatever 1.1 material you are looking at,
      then fine - but at least be aware that this is not the end of the
      story...

      Marc

      Comment

      • qglyirnyfgfo@mailinator.com

        #4
        Re: CollectionBase

        To add to Jon's post, this is how the property is internally
        implemented.

        protected IList List
        {
        get
        {
        return this;
        }
        }





        On Jun 10, 6:47 am, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
        On Jun 10, 12:39 pm, "Tony" <johansson.ande rs...@telia.com wrote:
        >
        Assume I have this classes shown below.
        Now to my question List below is inherited from CollectionBase and is a
        property and return an IList according to the documentation.
        I can't figure out if I do "Type myType1 = List.GetType(); " in the add
        method and check myType1 it shows name of Animals
        I thought it should show IList because List is of Type IList.
        >
        The *variable* List may be of type IList, but the actual object that
        the variable refers to can't be - you can't have objects which are
        "just" instances of an interface.
        >
        List.GetType() finds out the actual type of the object that List
        refers to.
        >
        Jon

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: CollectionBase

          On Jun 10, 3:44 pm, qglyirnyf...@ma ilinator.com wrote:
          To add to Jon's post, this is how the property is internally
          implemented.
          >
          protected IList List
          {
          get
          {
          return this;
          }
          >
          }
          Ah, I wasn't aware it was meant to be a complete example. In that case
          I'd like to correct my reply to refer to List as a property, not a
          variable :)

          Jon

          Comment

          Working...