about List and InnerList in class CollectionBase

T

Tony

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
 
M

Morten Wennevik [C# MVP]

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
}
}
 
T

Tony

Hello!

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

//Tony

Morten Wennevik said:
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 said:
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
 
M

Morten Wennevik [C# MVP]

Tony said:
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.
 
T

Tony

Hello!!

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

//Tony
 

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