471,325 Members | 1,421 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

Strange problem withCollectionBase

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
6 982
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
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
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
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
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
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.

Similar topics

2 posts views Thread by Paul Drummond | last post: by
7 posts views Thread by M O J O | last post: by
25 posts views Thread by Neil Ginsberg | last post: by
reply views Thread by unknown | last post: by
reply views Thread by Kris Vanherck | last post: by
11 posts views Thread by Martin Joergensen | last post: by
20 posts views Thread by SpreadTooThin | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.