CollectionBase

T

Tony

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.GetType();
then myType2 is an ArrayList which is correct.

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

Type myType1= List.GetType();

Type myType2= InnerList.GetType();


}
}


class Animal
{
something here
}

//Tony
 
J

Jon Skeet [C# MVP]

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
 
M

Marc Gravell

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
 
Q

qglyirnyfgfo

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

protected IList List
{
get
{
return this;
}
}
 
J

Jon Skeet [C# MVP]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top