Hi
Can any one help me
i am using queue class and i am is resizing by using TrimToSize(); how can i see that is it resized ....?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Jun 24, 2011, 10:25 AM
In the following program, we start with an empty queue with a capacity of 16.
We then enqueue a couple of strings, call TrimToSize() and find that the capacity has now been reduced to 2:
using System;
using System.Collections;
using System.Reflection;
class Test
{
static void Main()
{
Queue q = new Queue(16);
Type t = typeof(Queue);
BindingFlags bf = BindingFlags.Instance | BindingFlags.NonPublic;
FieldInfo fi = t.GetField ("_array", bf);
object[] oa = (object[])fi.GetValue(q);
Console.WriteLine(oa.Length); // 16
q.Enqueue("Hello");
q.Enqueue("World");
q.TrimToSize();
oa = (object[])fi.GetValue(q);
Console.WriteLine(oa.Length); // 2
Console.ReadKey();
}
}
VulpesPosted Jun 24, 2011, 9:40 AM
You might be able to do it using reflection by obtaining the length of its private _array field which is an object[].
If you really need it, I could try to figure something out.