सभी प्रारंभिक आकार provide a hint to the implementation to have at least a given capacity है। यह N
डिफ़ॉल्ट प्रविष्टियों से भरा एक सूची नहीं बनाता है; जोर मेरा:
Initializes a new instance of the List<T>
class that is empty and has the specified initial capacity.
आप टिप्पणी अनुभाग के लिए MSDN प्रविष्टि के माध्यम से जारी रखने के लिए हैं, तो आप यह क्यों निर्माता अधिभार प्रदान की जाती है मिल जाएगा (फिर से, जोर मेरा):
The capacity of a List<T>
is the number of elements that the List<T>
can hold. As elements are added to a List<T>
, the capacity is automatically increased as required by reallocating the internal array.
If the size of the collection can be estimated, specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the List<T>
.
कम List<T>.Count
में है List<T>.Capacity
के समान नहीं ("यदि तत्व जोड़ते समय क्षमता गणना से अधिक है, तो क्षमता बढ़ जाती है ...")।
आप क्योंकि इस सूची में केवल तार्किक आइटम आप जोड़ सकते हैं, शामिल हैं क्षमता को बदलने आइटम तार्किक संग्रहीत की संख्या में परिवर्तन नहीं होता अपवाद प्राप्त करते हैं।
Unhandled Exception: System.ArgumentOutOfRangeException: capacity was less than
the current size.
Parameter name: value
at System.Collections.Generic.List`1.set_Capacity(Int32 value)
शायद व्यवहार आप देख रहे हैं बनाने के लिए::
public static List<T> CreateDefaultList<T>(int entries)
{
return new List<T>(new T[entries]);
}
आप स्थिति 2 में सम्मिलित नहीं कर सकते, अगर पदों 0 और 1 अभी तक –
भरा नहीं कर रहे हैं के रूप में सही ढंग से बताया, क्षमता नहीं आकार के समान ही है । सम्मिलित करने से पहले कंसोल पर गणना लिखें, सूची में 3 आइटम कम हैं, इसलिए तीसरी स्थिति में नया मान डालना विफल रहता है। –