473,223 Members | 1,755 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,223 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 2101
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...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.