Strange problem withCollectionBase

T

Tony Johansson

Hello!

Assume I have this inheritance see below.
I know that a much better alternative is to use generics but I just want to
know how things works.

According to the documentation in CollectionBase for member List it return
an IList.
In the class Int16Collection I check by using method Test what this List in
CollectionBase is actually refering to and
it's refering to Int16Collection.
So according to that and the documenation we should have this kind of
situation.
IList List = refering to my instance of Int16Collection which is the same as
the keyword this.

So because this List is actually refering to an instance of Int16Collection
I should be able to use intellisense on this List which should display Foo
as a possible member to use but
as a matter of fact it doesn't if I cast to Int16Collection this Foo is
available.

But I can't understand this if List is actually refering to an instance to
Int16Collection
this Foo should be display when the intellisense is used on List.

public class Int16Collection : CollectionBase
{
public void Test()
{
Console.Writeln(List.GetType()); //It writes out Int16Collection
}

public void Foo()
{

}
}

I would be very glad is someone can explain this to me ?

//Tony
 
J

Jon Skeet [C# MVP]

Tony Johansson said:
Assume I have this inheritance see below.
I know that a much better alternative is to use generics but I just want to
know how things works.

According to the documentation in CollectionBase for member List it return
an IList.
In the class Int16Collection I check by using method Test what this List in
CollectionBase is actually refering to and
it's refering to Int16Collection.

The List property of CollectionBase always returns "this" (i.e. the
same instance).
But I can't understand this if List is actually refering to an instance to
Int16Collection
this Foo should be display when the intellisense is used on List.

No, because the *compile-time* type of the List property is "IList".
Intellisense is based on the knowledge at compile-time.

Here's another example:

object o = "Hello"
int i = o.Length;

That won't compile, because even though o actually refers to a string
at execution time, the compiler only know that it's of type "object"
which doesn't have a Length property.
 
T

Tony Johansson

Hello!!

If I have the construction below and I check the type for List by using the
CheckTypeOnList method
this methods WriteLine will display Int16Collection so here it must mean
that List is refering to class Int16Collection.
But according to you it should return *this* which is CollectionBase.
Have I missed something here when you wrote
The List property of CollectionBase always returns "this" (i.e. the
same instance).



public class Int16Collection : CollectionBase
{
public CheckTypeOnList()
{
Console.Wroteln(List.GetType());
}

}


//Tony
 
J

Jon Skeet [C# MVP]

Tony Johansson said:
If I have the construction below and I check the type for List by using the
CheckTypeOnList method
this methods WriteLine will display Int16Collection so here it must mean
that List is refering to class Int16Collection.

That's the execution-time type.
But according to you it should return *this* which is CollectionBase.

No, because you're calling it on Int16Collection. Change your code to

Console.WriteLine(this.GetType())

and you'll see Int16Collection as well.
 
T

Tony Johansson

Hello!

Have I misunderstood this.
If I use List.GetType it must mean that what is List refering to at
execution type and here
I get Int16Collection as an answer.
Is that correct?

So in that case what do you mean with this "The List property of
CollectionBase always returns "this" (i.e. the
same instance)." ?

public class Int16Collection : CollectionBase
{
public CheckTypeOnList()
{
Console.Wroteln(List.GetType());
}
}


//Tony
 
J

Jon Skeet [C# MVP]

Tony Johansson said:
Have I misunderstood this.
If I use List.GetType it must mean that what is List refering to at
execution type and here
I get Int16Collection as an answer.
Is that correct?
Yes.

So in that case what do you mean with this "The List property of
CollectionBase always returns "this" (i.e. the
same instance)." ?

I mean that the List property is implemented as:

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

Tony

Hello!

This may seem contradiction for me!

According to you is List in CollectionBase implemented something like the
below which you sent me. So the referenced type for List that will be
returned at execution time must be CollectionBase. The compile type for List
is IList
But when I looked at it by using method Test in Int16Collection(see below) I
get that the referenced type at execution type is Int16Collection.
So the referenced type from CollectionBase for List is not the same as the
referenced type for List when I use GetType in method Test.
What is it that I miss here ?

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

public class Int16Collection : CollectionBase
{
public void Test()
{
Console.Writeln(List.GetType()); //It writes out Int16Collection
}
}

//Tony
 

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