473,320 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 2104
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Majed | last post by:
hi all i've created a strong named collection which inherits collection base,but when i try to add to it a nullreferenceexception blows. the code is as listed below. do i have to init the list...
2
by: Colin Basterfield | last post by:
Hi, I have a list which is derived from CollectionBase, and it contains a list of User objects, which I want to Serialize out to an XML file. Is there anywhere where I can find how to decode...
2
by: Colin Basterfield | last post by:
Hi, I have two lists, the first is the complete list, so call it PermList, the second is a subsection of that list, call this TempList, both are of the same type, naemly strongly typed and...
3
by: jason | last post by:
Hello. I've got this simple collection populate code I downloaded from the net (sorry can't find source now) I'm trying to test, but I can't seem to get it to work. Any help would be greatly...
3
by: SStory | last post by:
For an owner drawn list box, I have a collection that represents some graphics objects in my app. I inherited the collection class from collectionbase. It would be nice to somehow just set the...
3
by: moondaddy | last post by:
I wrote my own List class which I use to bind to list controls. this class inherits CollectionBase and implements IBindingList. This class contains a list of business classes such as customers...
8
by: Yuk Tang | last post by:
I am tearing my hair out over this, since I can't see what I'm doing wrong (duh, if I knew, I wouldn't be asking the question). I am adding Field items to a Field Collection, but for some reason...
2
by: Demetri | last post by:
I have a collection class that inherits from CollectionBase. I am using the List.Remove method to remove an object in the collection. When I use it, I get the following error: Exception Details:...
5
by: Tony | last post by:
Hello! Here I have a collection class Cards which is derived from the Base class CollectionBase. This class Cards is a container for Card object. Now to my question at the bottom of this class...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.