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 ....?
Queue
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted Jun 24, 2011, 5:40 AM EDT
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.
VulpesRank #55 · 29.2K reputationPosted Jun 24, 2011, 6:25 AM EDT
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();
}
}