ArrayList to List<myType> and item disposal

Hi All,


Suppose you need to release unmanaged resources when you remove an
item from a colletction.

We are now using an override of the ArrayList.Remove() method that
dows the job.

We wanted to move to generics but the List<T> collection does not
feature any virtual method.

How should we proceed?

We need to release unmanaged resources immediately and cannot rely on
the GC delayed cleanup.


Thanks!

Alberto
info [ Mo, 28 Januar 2008 09:23 ] [ ID #1917811 ]

Re: ArrayList to List<myType> and item disposal

Collection<T> provides the necessary virtual methods - but it will be
up to your code to ensure that nothing else (other than the
collection) is still using it when it gets disposed.

Marc
Marc Gravell [ Mo, 28 Januar 2008 09:33 ] [ ID #1917813 ]

Re: ArrayList to List<myType> and item disposal

Thanks a lot Marc, it is exaclty what I am looking for !!!!
info [ Di, 29 Januar 2008 09:10 ] [ ID #1918776 ]

Re: ArrayList to List<myType> and item disposal

Hi Mark, it looked so straightforward but the crude reality is back:

No way to set collection capacity or to resize it: if you need to
store 1,000,000 items, it would require too many re-alloc

Do you know a workaround?

Can I find the Collection<T> class source code somewhere?

Thanks,

Alberto
info [ Di, 29 Januar 2008 10:36 ] [ ID #1918778 ]

Re: ArrayList to List<myType> and item disposal

> it would require too many re-alloc
The size uses a doubling strategy, so it won't be as bad as you think.
However, if you want to have full control, note that actually the
collection just *wraps* a list (which is created for you normally) -
you could use the alternative constructor.

The following doesn't show a huge increase for doing this... but I
guess it minimises the work of the GC and the amount of excess...

class MyCollection<T> : Collection<T> {
public MyCollection() { }
public MyCollection(int capacity) : base(new List<T>(capacity))
{ }
}

static class Program {

static void Main() {
MyCollection<int> col = new MyCollection<int>();
Fill(col, "Using default sizing");
col = new MyCollection<int>(1000000);
Fill(col, "Preallocating");
}
static void Fill(MyCollection<int> col, string message) {
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < 1000000; i++) {
col.Add(i);
}
watch.Stop();
Console.WriteLine(message);
Console.WriteLine(col.Count);
Console.WriteLine(watch.ElapsedMilliseconds);
}
}
Marc Gravell [ Di, 29 Januar 2008 11:03 ] [ ID #1918780 ]

Re: ArrayList to List<myType> and item disposal

Nice, but in many cases we resize it to 100,000 and use 90,000. In
your sample this is not possible. In addition resizing would clear the
array contents. I hope that something is changed in .NET 3.5. Do you
know if Collection<T> in .NET 3.5 is more capable?

Thanks again,

Alberto
info [ Mi, 30 Januar 2008 09:24 ] [ ID #1919805 ]

Re: ArrayList to List<myType> and item disposal

> but in many cases we resize it to 100,000 and use 90,000
You mean you want something like List<T>.Capacity? Then add it! See
sample below (the trick here is to keep a handle to the underlying
list that Collection<T> wraps).

> In addition resizing would clear the array contents.
If you want to clear the contents, call Clear();

> Do you know if Collection<T> in .NET 3.5 is more capable?
Well, I'm not sure I agree that it is lacking - but Collection<T> is
"red bits" - so there are no changes *at all* in 3.0 or 3.5 (which
just adds extra assemblies - it doesn't update any); likewise there
are no changes in 2.0 SP1 that I am aware of.

Marc

===Sample===

sealed class MyCollection<T> : Collection<T> {
private readonly List<T> baseList;

private MyCollection(List<T> baseList)
: base(baseList) {
this.baseList = baseList;
}
public MyCollection() : this(new List<T>()) { }
public MyCollection(int capacity) : this(new List<T>(capacity))
{ }

public int Capacity {
get {return baseList.Capacity;}
set {baseList.Capacity = value;}
}
}
Marc Gravell [ Do, 31 Januar 2008 09:04 ] [ ID #1920638 ]

Re: ArrayList to List<myType> and item disposal

Thanks so much Marc, you are great! Where did you learn these tricks?

I'll try again an let you know.

Thanks!

Alberto
info [ Fr, 01 Februar 2008 09:06 ] [ ID #1921549 ]

Re: ArrayList to List<myType> and item disposal

> Thanks so much Marc
No problem; I'm glad it helped

> Where did you learn these tricks?
Trial and error (mainly error...); and by watching / asking questions
on forums like this.

Marc
Marc Gravell [ Fr, 01 Februar 2008 11:10 ] [ ID #1921553 ]
Microsoft » microsoft.public.dotnet.general » ArrayList to List<myType> and item disposal

Vorheriges Thema: Panoramic Image creation in .Net (Image Stitching)?
Nächstes Thema: insert multiple rows