Questa documentazione è stata archiviata e non viene gestita.

Metodo SortedList.Clear

Consente di rimuovere tutti gli elementi dalla SortedList.

Spazio dei nomi: System.Collections
Assembly: mscorlib (in mscorlib.dll)

public virtual void Clear ()
public void Clear ()
public function Clear ()

Tipo di eccezioneCondizione

NotSupportedException

L'SortedList è in sola lettura.

-oppure->

L'oggetto SortedList è di dimensioni fisse.

La proprietà Count è impostata su zero e vengono rilasciati riferimenti ad altri oggetti da elementi dell'insieme.

La proprietà Capacity rimane invariata. Per reimpostare la capacità di SortedList, chiamare TrimToSize oppure impostare la proprietà Capacity direttamente. Se si esegue un'operazione di taglio su una classe SortedList vuota, la capacità di tale classe SortedList viene impostata sulla capacità predefinita.

Questo metodo è un'operazione O(n), dove n corrisponde a Count.

Nell'esempio seguente viene illustrato come eseguire l'operazione di trim delle parti inutilizzate di SortedList e come cancellare i valori di SortedList.

using System;
using System.Collections;
public class SamplesSortedList  {

   public static void Main()  {

      // Creates and initializes a new SortedList.
      SortedList mySL = new SortedList();
      mySL.Add( "one", "The" );
      mySL.Add( "two", "quick" );
      mySL.Add( "three", "brown" );
      mySL.Add( "four", "fox" );
      mySL.Add( "five", "jumped" );

      // Displays the count, capacity and values of the SortedList.
      Console.WriteLine( "Initially," );
      Console.WriteLine( "   Count    : {0}", mySL.Count );
      Console.WriteLine( "   Capacity : {0}", mySL.Capacity );
      Console.WriteLine( "   Values:" );
      PrintKeysAndValues( mySL );

      // Trims the SortedList.
      mySL.TrimToSize();

      // Displays the count, capacity and values of the SortedList.
      Console.WriteLine( "After TrimToSize," );
      Console.WriteLine( "   Count    : {0}", mySL.Count );
      Console.WriteLine( "   Capacity : {0}", mySL.Capacity );
      Console.WriteLine( "   Values:" );
      PrintKeysAndValues( mySL );

      // Clears the SortedList.
      mySL.Clear();

      // Displays the count, capacity and values of the SortedList.
      Console.WriteLine( "After Clear," );
      Console.WriteLine( "   Count    : {0}", mySL.Count );
      Console.WriteLine( "   Capacity : {0}", mySL.Capacity );
      Console.WriteLine( "   Values:" );
      PrintKeysAndValues( mySL );

      // Trims the SortedList again.
      mySL.TrimToSize();

      // Displays the count, capacity and values of the SortedList.
      Console.WriteLine( "After the second TrimToSize," );
      Console.WriteLine( "   Count    : {0}", mySL.Count );
      Console.WriteLine( "   Capacity : {0}", mySL.Capacity );
      Console.WriteLine( "   Values:" );
      PrintKeysAndValues( mySL );
   }


   public static void PrintKeysAndValues( SortedList myList )  {
      Console.WriteLine( "\t-KEY-\t-VALUE-" );
      for ( int i = 0; i < myList.Count; i++ )  {
         Console.WriteLine( "\t{0}:\t{1}", myList.GetKey(i), myList.GetByIndex(i) );
      }
      Console.WriteLine();
   }
}
/* 
This code produces the following output.

Initially,
   Count    : 5
   Capacity : 16
   Values:
    -KEY-    -VALUE-
    five:    jumped
    four:    fox
    one:    The
    three:    brown
    two:    quick

After TrimToSize,
   Count    : 5
   Capacity : 5
   Values:
    -KEY-    -VALUE-
    five:    jumped
    four:    fox
    one:    The
    three:    brown
    two:    quick

After Clear,
   Count    : 0
   Capacity : 16
   Values:
    -KEY-    -VALUE-

After the second TrimToSize,
   Count    : 0
   Capacity : 16
   Values:
    -KEY-    -VALUE-
*/ 

import System.*;
import System.Collections.*;

public class SamplesSortedList
{
    public static void main(String[] args)
    {
        // Creates and initializes a new SortedList.
        SortedList mySL = new SortedList();

        mySL.Add("one", "The");
        mySL.Add("two", "quick");
        mySL.Add("three", "brown");
        mySL.Add("four", "fox");
        mySL.Add("five", "jumped");

        // Displays the count, capacity and values of the SortedList.
        Console.WriteLine("Initially,");
        Console.WriteLine("   Count    : {0}", 
            System.Convert.ToString(mySL.get_Count()));
        Console.WriteLine("   Capacity : {0}", 
            System.Convert.ToString(mySL.get_Capacity()));
        Console.WriteLine("   Values:");
        PrintKeysAndValues(mySL);

        // Trims the SortedList.
        mySL.TrimToSize();

        // Displays the count, capacity and values of the SortedList.
        Console.WriteLine("After TrimToSize,");
        Console.WriteLine("   Count    : {0}", 
            System.Convert.ToString(mySL.get_Count()));
        Console.WriteLine("   Capacity : {0}", 
            System.Convert.ToString(mySL.get_Capacity()));
        Console.WriteLine("   Values:");
        PrintKeysAndValues(mySL);

        // Clears the SortedList.
        mySL.Clear();

        // Displays the count, capacity and values of the SortedList.
        Console.WriteLine("After Clear,");
        Console.WriteLine("   Count    : {0}", 
            System.Convert.ToString(mySL.get_Count()));
        Console.WriteLine("   Capacity : {0}", 
            System.Convert.ToString(mySL.get_Capacity()));
        Console.WriteLine("   Values:");
        PrintKeysAndValues(mySL);

        // Trims the SortedList again.
        mySL.TrimToSize();

        // Displays the count, capacity and values of the SortedList.
        Console.WriteLine("After the second TrimToSize,");
        Console.WriteLine("   Count    : {0}", 
            System.Convert.ToString(mySL.get_Count()));
        Console.WriteLine("   Capacity : {0}", 
            System.Convert.ToString(mySL.get_Capacity()));
        Console.WriteLine("   Values:");
        PrintKeysAndValues(mySL);
    } //main

    public static void PrintKeysAndValues(SortedList myList)
    {
        Console.WriteLine("\t-KEY-\t-VALUE-");
        for (int i = 0; i < myList.get_Count(); i++) {
            Console.WriteLine("\t{0}:\t{1}", myList.GetKey(i), 
                myList.GetByIndex(i));
        }
        Console.WriteLine();
    } //PrintKeysAndValues

} //SamplesSortedList

/* 
 This code produces the following output.
 
 Initially,
    Count    : 5
    Capacity : 16
    Values:
     -KEY-    -VALUE-
     five:    jumped
     four:    fox
     one:    The
     three:    brown
     two:    quick
 
 After TrimToSize,
    Count    : 5
    Capacity : 5
    Values:
     -KEY-    -VALUE-
     five:    jumped
     four:    fox
     one:    The
     three:    brown
     two:    quick
 
 After Clear,
    Count    : 0
    Capacity : 16
    Values:
     -KEY-    -VALUE-
 
 After the second TrimToSize,
    Count    : 0
    Capacity : 16
    Values:
     -KEY-    -VALUE-
 */

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile per Pocket PC, Windows Mobile per Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework non supporta tutte le versioni di ciascuna piattaforma. Per un elenco delle versioni supportate, vedere Requisiti di sistema.

.NET Framework

Supportato in: 2.0 1.1 1.0

.NET Compact Framework

Supportato in: 2.0
Mostra: