ArrayList क्लास में Capacity गुण उन एलिमेंट्स की संख्या को प्राप्त या सेट करता है जिन्हें ArrayList समाहित कर सकता है।
Capacity हमेशा Count से अधिक होती है। Capacity गुण के लिए:
arrList.Capacity
डिफ़ॉल्ट क्षमता 4 होती है। यदि 5 एलिमेंट्स हैं, तो इसकी क्षमता दोगुनी होकर 8 हो जाएगी। यह क्रम चलता रहता है।
आप C# में Capacity गुण को कार्यान्वित करने के लिए निम्न कोड चलाने का प्रयास कर सकते हैं। यह यह भी दिखाता है कि हमने ऊपर क्या चर्चा की:
उदाहरण
using System; using System.Collections; class Demo { public static void Main() { ArrayList arrList = new ArrayList(); arrList.Add(19); arrList.Add(44); arrList.Add(22); ArrayList arrList2 = new ArrayList(); arrList2.Add(19); arrList2.Add(44); arrList2.Add(64); arrList2.Add(32); arrList2.Add(99); Console.WriteLine("ArrayList1 - Total elements: "+arrList.Count); Console.WriteLine("ArrayList1 - Capacity: "+arrList.Capacity); Console.WriteLine("ArrayList2 - Total elements: "+arrList2.Count); Console.WriteLine("ArrayList2 - Capacity: "+arrList2.Capacity); } }आउटपुट
ArrayList1 - Total elements: 3 ArrayList1 - Capacity: 4 ArrayList2 - Total elements: 5 ArrayList2 - Capacity: 8