C# | Establecer la capacidad en el número real de elementos en un objeto SortedList – Part 1

El método SortedList.TrimToSize se usa para establecer la capacidad en el número real de elementos en un objeto SortedList.
Sintaxis: 
 

public virtual void TrimToSize ();

Excepción: este método generará NotSupportedException si el objeto SortedList es de solo lectura o si SortedList tiene un tamaño fijo .
Los siguientes programas ilustran el uso del método mencionado anteriormente:
Ejemplo 1:
 

CSharp

// C# program to illustrate the
// SortedList.TrimToSize() Method
using System;
using System.Collections;
 
class GFG {
 
    // Main method
    public static void Main()
    {
 
        // create and initialize new SortedList
        SortedList mylist = new SortedList();
 
        // Adding elements to SortedList
        mylist.Add("1", "C++");
        mylist.Add("2", "Java");
        mylist.Add("3", "DSA");
        mylist.Add("4", "Python");
        mylist.Add("5", "C#");
        mylist.Add("6", "HTML");
 
        // Capacity of SortedList before trimmimg
        Console.WriteLine("Before trimming the capacity is: {0}",
                                                mylist.Capacity);
 
        // trim the SortedList
        mylist.TrimToSize();
 
        // Capacity of ArrayList after trimming
        Console.WriteLine("After trimming the capacity is: {0}",
                                               mylist.Capacity);
    }
}
Producción: 

Before trimming the capacity is: 16
After trimming the capacity is: 6

 

Ejemplo 2:

CSharp

// C# program to illustrate
// SortedList.TrimToSize() Method
using System;
using System.Collections;
 
class GFG {
 
    // Main Method
    public static void Main()
    {
 
        // create and initialize new SortedList
        SortedList mylist = new SortedList();
 
        // Adding elements to SortedList
        mylist.Add("h", "Hello");
        mylist.Add("g", "Geeks!");
        mylist.Add("w", "Welcome");
        mylist.Add("t", "to");
        mylist.Add("n", "Noida");
 
        // Displaying the count, capacity
        // and values of the SortedList.
        Console.WriteLine("Before Using TrimToSize Method:");
        Console.WriteLine();
        Console.WriteLine("Count: {0}", mylist.Count);
        Console.WriteLine("Capacity: {0}", mylist.Capacity);
        Console.Write("Values are: ");
        Display(mylist);
 
        Console.WriteLine();
 
        // Trim the SortedList.
        mylist.TrimToSize();
 
        // Displaying the count, capacity
        // and values of the SortedList.
        Console.WriteLine();
        Console.WriteLine("After Using TrimToSize Method:");
        Console.WriteLine();
        Console.WriteLine("Count: {0}", mylist.Count);
        Console.WriteLine("Capacity: {0}", mylist.Capacity);
        Console.Write("Values are: ");
        Display(mylist);
 
        // Clear the SortedList
        mylist.Clear();
 
        Console.WriteLine();
 
        // Displaying the count, capacity
        // and values of the SortedList.
        Console.WriteLine();
        Console.WriteLine("After Using Clear Method:");
        Console.WriteLine();
        Console.WriteLine("Count: {0}", mylist.Count);
        Console.WriteLine("Capacity: {0}", mylist.Capacity);
        Console.Write("Values are: ");
        Display(mylist);
 
        // again trim the SortedList
        mylist.TrimToSize();
 
        Console.WriteLine();
 
        // Displaying the count, capacity
        // and values of the SortedList.
        Console.WriteLine();
        Console.WriteLine("After Again Using TrimToSize Method:");
        Console.WriteLine();
        Console.WriteLine("Count: {0}", mylist.Count);
        Console.WriteLine("Capacity: {0}", mylist.Capacity);
        Console.Write("Values are: ");
        Display(mylist);
    }
 
    // to display the values of SortedList
    public static void Display(SortedList mylist)
    {
        // taking an IList and
        // using GetValueList method
        IList vlist = mylist.GetValueList();
 
        for (int i = 0; i < mylist.Count; i++)
            Console.Write(vlist[i] + " ");
    }
}
Producción: 

Before Using TrimToSize Method:

Count: 5
Capacity: 16
Values are: Geeks! Hello Noida to Welcome 

After Using TrimToSize Method:

Count: 5
Capacity: 5
Values are: Geeks! Hello Noida to Welcome 

After Using Clear Method:

Count: 0
Capacity: 5
Values are: 

After Again Using TrimToSize Method:

Count: 0
Capacity: 0
Values are:

 

Nota:  

  • El uso principal de este método es que puede usarse para minimizar la sobrecarga de memoria de una colección si no se agregarán nuevos elementos a la colección.
  • Para restablecer un objeto SortedList a su estado inicial, llame al método Clear antes de llamar a TrimToSize. Recortar una SortedList vacía establece la capacidad de la SortedList en la capacidad predeterminada.
  • Este método es una operación O(n), donde n es Count.

Referencia: 
 

Publicación traducida automáticamente

Artículo escrito por Kirti_Mangal y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *