List in this CollectionBase

T

Tony Johansson

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
 
J

Jon Skeet [C# MVP]

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
 
T

Tony Johansson

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]" <[email protected]> skrev i meddelandet
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
 
J

Jon Skeet [C# MVP]

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
 

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