SortedList.Capacity Property is used to get or set the capacity of a SortedList object.
Syntax:
1 2 3 |
public virtual int Capacity { get; set; } |
Return Value: This property returns the number of elements of type System.Int32 that the SortedList object can contain.
Exceptions:
- ArgumentOutOfRangeException: If the value assigned is less than the current number of elements in the SortedList object.
- OutOfMemoryException: If there is not enough memory available on the system.
Capacity Vs Count:
- Count is always less than the Capacity. While adding the elements, if Count exceeds Capacity then the Capacity will increase automatically by reallocating the internal array before copying the old elements and adding the new elements.
- Capacity is the number of the elements which the List can store before resizing of List needed. But Count is the number of the elements which are actually present in the List.
- If the Capacity is much larger than the Count the user can decrease capacity by either calling the TrimExcess method or explicitly setting the Capacity to a lower value.
- If the Capacity is settled explicitly then the internal array is also reallocated to accommodate the specified capacity, and all the elements are copied.
- Retrieving the value of the Capacity property is an O(1) operation while setting the Capacity is an O(n) operation, where n is a new capacity.
Below given are some examples to understand the implementation in a better way:
Example 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
// C# program to illustrate the // Capacity Property of SortedList using System; using System.Collections; class cce{ // Main Method public static void Main(String[] args) { // Creating a SortedList of integers // Here we are not setting // Capacity explicitly SortedList mySortedList = new SortedList(); // Adding elements to SortedList mySortedList.Add("1", "C#"); mySortedList.Add("2", "Java"); mySortedList.Add("3", "DSA"); mySortedList.Add("4", "Python"); mySortedList.Add("5", "C"); // Printing the Capacity of mySortedList Console.WriteLine("Capacity Is: " + mySortedList.Capacity); // Printing the Count of mySortedList Console.WriteLine("Count Is: " + mySortedList.Count); // Adding some more // elements in mySortedList mySortedList.Add("6", "C++"); mySortedList.Add("7", "HTML"); mySortedList.Add("8", "CSS"); mySortedList.Add("9", "Web"); mySortedList.Add("10", "DBMS"); mySortedList.Add("11", "CN"); mySortedList.Add("12", "Ruby"); mySortedList.Add("13", "Perl"); mySortedList.Add("14", "R"); mySortedList.Add("15", "GO"); mySortedList.Add("16", "Scala"); mySortedList.Add("17", "Big Data"); // Printing the Capacity of mySortedList // It will give output 32 as internally // SortedList is resized Console.WriteLine("Capacity Is: " + mySortedList.Capacity); // Printing the Count of mySortedList Console.WriteLine("Count Is: " + mySortedList.Count); } } |
Output:
[…] Capacity […]