473,387 Members | 1,579 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,387 software developers and data experts.

about List and InnerList in class CollectionBase

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 we have a method called
Contains.
It gived the same result to use InnerList and List in this method Contains.
I can also just replace List with InnerList in the method Add and it works.

So is the InnerList the same as List because I can use whichever of these
two ?
public class Cards : CollectionBase
{
public Cards() {}

public void Add(Card newCard)
{
List.Add(newCard);
}

public void Remove(Card oldCard)
{
List.Remove(oldCard);
}

public Card this[int cardIndex]
{
get { return (Card)List[cardIndex]; }
set { List[cardIndex] = value; }
}

public void CopyTo(Cards targetCards)
{
for (int index = 0; index < this.Count; index++)
targetCards[index] = this[index];
}

public bool Contains(Card card)
{
bool test1,test2;
test1 = InnerList.Contains(card);
test2 = List.Contains(card);
return true;

}
}

//Tony
Jun 27 '08 #1
5 4808
Hi Tony,

The difference is that InnerList returns a losely connected ArrayList which
will not trigger the On* methods when manipulated. List will return an IList
which will trigger the On* methods when manipulated.

class MyCollection : CollectionBase
{
protected override void OnInsert(int index, object value)
{
base.OnInsert(index, value);
}

public void Add(object value)
{
List.Add(value); // Will trigger OnInsert
InnerList.Add(value); // Will not trigger OnInsert
}
}
--
Happy Coding!
Morten Wennevik [C# MVP]
"Tony" wrote:
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 we have a method called
Contains.
It gived the same result to use InnerList and List in this method Contains.
I can also just replace List with InnerList in the method Add and it works.

So is the InnerList the same as List because I can use whichever of these
two ?
public class Cards : CollectionBase
{
public Cards() {}

public void Add(Card newCard)
{
List.Add(newCard);
}

public void Remove(Card oldCard)
{
List.Remove(oldCard);
}

public Card this[int cardIndex]
{
get { return (Card)List[cardIndex]; }
set { List[cardIndex] = value; }
}

public void CopyTo(Cards targetCards)
{
for (int index = 0; index < this.Count; index++)
targetCards[index] = this[index];
}

public bool Contains(Card card)
{
bool test1,test2;
test1 = InnerList.Contains(card);
test2 = List.Contains(card);
return true;

}
}

//Tony
Jun 27 '08 #2
Hello!

Will it be some performance effect if a use List insted of InnerList when
not having any OnInsert(...)

//Tony

"Morten Wennevik [C# MVP]" <Mo************@hotmail.comskrev i meddelandet
news:B9**********************************@microsof t.com...
Hi Tony,

The difference is that InnerList returns a losely connected ArrayList
which
will not trigger the On* methods when manipulated. List will return an
IList
which will trigger the On* methods when manipulated.

class MyCollection : CollectionBase
{
protected override void OnInsert(int index, object value)
{
base.OnInsert(index, value);
}

public void Add(object value)
{
List.Add(value); // Will trigger OnInsert
InnerList.Add(value); // Will not trigger OnInsert
}
}
--
Happy Coding!
Morten Wennevik [C# MVP]
"Tony" wrote:
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 we have a method called
Contains.
It gived the same result to use InnerList and List in this method
Contains.
I can also just replace List with InnerList in the method Add and it
works.

So is the InnerList the same as List because I can use whichever of
these
two ?
public class Cards : CollectionBase
{
public Cards() {}

public void Add(Card newCard)
{
List.Add(newCard);
}

public void Remove(Card oldCard)
{
List.Remove(oldCard);
}

public Card this[int cardIndex]
{
get { return (Card)List[cardIndex]; }
set { List[cardIndex] = value; }
}

public void CopyTo(Cards targetCards)
{
for (int index = 0; index < this.Count; index++)
targetCards[index] = this[index];
}

public bool Contains(Card card)
{
bool test1,test2;
test1 = InnerList.Contains(card);
test2 = List.Contains(card);
return true;

}
}

//Tony


Jun 27 '08 #3

"Tony" wrote:
Hello!

Will it be some performance effect if a use List insted of InnerList when
not having any OnInsert(...)

//Tony
There is a tiny overhead when using List vs InnerList, but it is only
visible when using large collections (>1 000 000 items) and even then
measurable in milliseconds. Most likely this wouldn't be the bottleneck in
performance issues even then. If you need to override
CollectionBase-behaviour, use List, if you don't, or if you want to
circumvent overridden behaviour, use InnerList.

--
Happy Coding!
Morten Wennevik [C# MVP]

Jun 27 '08 #4
Hello!!

I just wonder does this OnInsert(...)
actually do anything when I'm not overriding it.

//Tony

"Morten Wennevik [C# MVP]" <Mo************@hotmail.comskrev i meddelandet
news:2B**********************************@microsof t.com...
>
"Tony" wrote:
Hello!

Will it be some performance effect if a use List insted of InnerList
when
not having any OnInsert(...)

//Tony

There is a tiny overhead when using List vs InnerList, but it is only
visible when using large collections (>1 000 000 items) and even then
measurable in milliseconds. Most likely this wouldn't be the bottleneck
in
performance issues even then. If you need to override
CollectionBase-behaviour, use List, if you don't, or if you want to
circumvent overridden behaviour, use InnerList.

--
Happy Coding!
Morten Wennevik [C# MVP]

Jun 27 '08 #5
Hi Tony,

I'm not sure if anything happens during the On* events. If you are using
Visual Studio 2008 you can easily see for yourself by stepping into framework
code. See the article below for an example on how to configure Visual Studio
2008 to enable this

http://blogs.msdn.com/sburke/archive...urce-code.aspx

--
Happy Coding!
Morten Wennevik [C# MVP]
"Tony" wrote:
Hello!!

I just wonder does this OnInsert(...)
actually do anything when I'm not overriding it.

//Tony

"Morten Wennevik [C# MVP]" <Mo************@hotmail.comskrev i meddelandet
news:2B**********************************@microsof t.com...

"Tony" wrote:
Hello!
>
Will it be some performance effect if a use List insted of InnerList
when
not having any OnInsert(...)
>
//Tony
>
There is a tiny overhead when using List vs InnerList, but it is only
visible when using large collections (>1 000 000 items) and even then
measurable in milliseconds. Most likely this wouldn't be the bottleneck
in
performance issues even then. If you need to override
CollectionBase-behaviour, use List, if you don't, or if you want to
circumvent overridden behaviour, use InnerList.

--
Happy Coding!
Morten Wennevik [C# MVP]



Jun 27 '08 #6

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...
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: 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...
9
by: me | last post by:
Hi All, I am new to Classes and learniing the ropes with VB.NET express Here's my question - say I have a want to manage a list of books. Each book has an Author, Title and ISBN Now, I am...
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:...
2
by: adriaandavel | last post by:
Hi all, I am trying to use a collection of String Arrays in an inherited instance of CollectionBase, but the InnerList.IndexOf does not seem to work, any ideas? My code is: public void...
3
by: Tony Johansson | last post by:
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.