471,310 Members | 1,375 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

List in this CollectionBase

Hello!

Sorry for opening up this task again.

I want to fully understand this List that is return from CollectionBase.

According to you is List in CollectionBase implemented something like the
below which you sent me previously. 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.

Can you explain whay the types are not the same?

< 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 #1
3 2021
On Jun 30, 1:25*pm, "Tony Johansson" <johansson.anders...@telia.com>
wrote:
Sorry for opening up this task again.
I'm happy to go over it again, but it would have been nicer to keep it
in the same thread as before.
I want to fully understand this List that is return from CollectionBase.

According to you is List in CollectionBase implemented something like the
below which you sent me previously. So the referenced type for List that
will be returned at execution time must be CollectionBase.
No, because it's returning a reference to the object it's called on. I
think you may have some misunderstandings about inheritance and what
"this" means. Let's ignore CollectionBase completely for the moment
and look at a standalone example:

using System;

public abstract class Fruit
{
public Fruit ReturnThis()
{
return this;
}
}

public class Apple : Fruit {}
public class Banana : Fruit {}

public class Test
{
static void Main()
{
Apple apple = new Apple();
Console.WriteLine(apple.ReturnThis().GetType());

Banana banana = new Banana();
Console.WriteLine(banana.ReturnThis().GetType());
}
}

Basically apple.ReturnThis() will return the same reference as the
value of the apple variable, and banana.ReturnThis() will return the
same reference as the value of the banana variable. So apple.GetType()
will always be the same as apple.ReturnThis().GetType().

Does that help?

Jon
Jun 30 '08 #2
Hello!

Very good explained by sending a simple example.
Now I understand why this List is displayed as it does in my Test method.

One more question.
By the way I find that List and InnerList to be very similar.

According to the documentation is says for InnerList
"gets an ArrayList containing the list of elements in the CollectionBase."
and for
List is says
"gets an IList containg the list of elements in the CollectionBase."

For exampel I can add by using both.

So what exactly is it that differ within the implementaion in the
CollectionBase between List and InnerList-

They can't be the same?

//Tony

"Jon Skeet [C# MVP]" <sk***@pobox.comskrev i meddelandet
news:4e**********************************@x35g2000 hsb.googlegroups.com...
On Jun 30, 1:25 pm, "Tony Johansson" <johansson.anders...@telia.com>
wrote:
Sorry for opening up this task again.
I'm happy to go over it again, but it would have been nicer to keep it
in the same thread as before.
I want to fully understand this List that is return from CollectionBase.

According to you is List in CollectionBase implemented something like the
below which you sent me previously. So the referenced type for List that
will be returned at execution time must be CollectionBase.
No, because it's returning a reference to the object it's called on. I
think you may have some misunderstandings about inheritance and what
"this" means. Let's ignore CollectionBase completely for the moment
and look at a standalone example:

using System;

public abstract class Fruit
{
public Fruit ReturnThis()
{
return this;
}
}

public class Apple : Fruit {}
public class Banana : Fruit {}

public class Test
{
static void Main()
{
Apple apple = new Apple();
Console.WriteLine(apple.ReturnThis().GetType());

Banana banana = new Banana();
Console.WriteLine(banana.ReturnThis().GetType());
}
}

Basically apple.ReturnThis() will return the same reference as the
value of the apple variable, and banana.ReturnThis() will return the
same reference as the value of the banana variable. So apple.GetType()
will always be the same as apple.ReturnThis().GetType().

Does that help?

Jon
Jun 30 '08 #3
On Jun 30, 2:48*pm, "Tony Johansson" <johansson.anders...@telia.com>
wrote:
Very good explained by sending a simple example.
Now I understand why this List is displayed as it does in my Test method.

One more question.
By the way I find that List and InnerList to be very similar.
No, they're somewhat different. List is the wrapper, which doesn't
actually contain the data at all but allows constraints to be applied
to data which is added to the inner list.
InnerList is just the data itself, with no smarts around what can be
stored.
According to the documentation is says for InnerList
"gets an ArrayList containing the list of elements in the CollectionBase."
and for List is says "gets an IList containg the list of elements in the CollectionBase."
Yes, they're both IList implementations with the same data - but with
different roles.
For exampel I can add by using both.

So what exactly is it that differ within the implementaion in the
CollectionBase between List and InnerList-

They can't be the same?
No, they'll never be the same. List delegates the actual storage to
InnerList.

Jon
Jun 30 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Majed | last post: by
2 posts views Thread by Colin Basterfield | last post: by
2 posts views Thread by Colin Basterfield | last post: by
3 posts views Thread by SStory | last post: by
2 posts views Thread by Demetri | 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.