By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,897 Members | 1,118 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,897 IT Pros & Developers. It's quick & easy.

Strange problem withCollectionBase

P: n/a
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

Jun 29 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Tony Johansson <jo*****************@telia.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jun 29 '08 #2

P: n/a
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

"Jon Skeet [C# MVP]" <sk***@pobox.comskrev i meddelandet
news:MP*********************@msnews.microsoft.com. ..
Tony Johansson <jo*****************@telia.comwrote:
>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.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com

Jun 29 '08 #3

P: n/a
Tony Johansson <jo*****************@telia.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jun 29 '08 #4

P: n/a
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
"Jon Skeet [C# MVP]" <sk***@pobox.comskrev i meddelandet
news:MP*********************@msnews.microsoft.com. ..
Tony Johansson <jo*****************@telia.comwrote:
>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.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com

Jun 29 '08 #5

P: n/a
Tony Johansson <jo*****************@telia.comwrote:
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;
}
}

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jun 29 '08 #6

P: n/a
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
"Jon Skeet [C# MVP]" <sk***@pobox.comskrev i meddelandet
news:MP*********************@msnews.microsoft.com. ..
Tony Johansson <jo*****************@telia.comwrote:
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;
}
}

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com

Jun 30 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.