C# | Obtener el número de elementos en el SortedSet

La clase SortedSet representa la colección de objetos ordenados. Esta clase viene bajo el espacio de nombres System.Collections.Generic . SortedSet<T>.Count Property se usa para obtener el número de elementos en el SortedSet.

Propiedades:

  • En C#, la clase SortedSet se puede usar para almacenar, eliminar o ver elementos.
  • Mantiene el orden ascendente y no almacena elementos duplicados.
  • Se sugiere usar la clase SortedSet si tiene que almacenar elementos únicos y mantener un orden ascendente.

Sintaxis:

mySortedSet.Count

Aquí, mySortedSet es un SortedSet.

A continuación se dan algunos ejemplos para entender la implementación de una mejor manera:

Ejemplo 1:

// C# code to get the number of
// elements in the SortedSet
using System;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a SortedSet of integers
        SortedSet<int> mySortedSet = new SortedSet<int>();
  
        // adding elements in mySortedSet
        mySortedSet.Add(1);
        mySortedSet.Add(2);
        mySortedSet.Add(3);
        mySortedSet.Add(4);
        mySortedSet.Add(5);
  
        // Displaying the number of elements in
        // the SortedSet using "Count" function
        Console.WriteLine("The number of elements in mySortedSet are: " 
                                                  + mySortedSet.Count);
    }
}
Producción:

The number of elements in mySortedSet are: 5

Ejemplo 2:

// C# code to get the number of
// elements in the SortedSet
using System;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a SortedSet of strings
        SortedSet<string> mySortedSet = new SortedSet<string>();
  
        // adding elements in mySortedSet
        mySortedSet.Add("Hey");
        mySortedSet.Add("GeeksforGeeks");
        mySortedSet.Add("and");
        mySortedSet.Add("Geeks Classes");
  
        // Displaying the number of elements in
        // the SortedSet using "Count" function
        Console.WriteLine("The number of elements in mySortedSet are: " 
                                                   + mySortedSet.Count);
    }
}
Producción:

The number of elements in mySortedSet are: 4

Referencia:

Publicación traducida automáticamente

Artículo escrito por Sahil_Bansall 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 *