C# | Obtener el número de strings en StringCollection – Part 1

La clase StringCollection es una nueva adición a la biblioteca de clases de .NET Framework que representa una colección de strings. La clase StringCollection se define en el espacio de nombres System.Collections.Specialized .

La propiedad StringCollection.Count se usa para obtener el número de strings contenidas en StringCollection.

Sintaxis:

public int Count { get; }

Valor devuelto: Devuelve el número de strings contenidas en StringCollection.

Nota: Recuperar el valor de esta propiedad es una operación O(1).

Los siguientes programas ilustran el uso de la propiedad StringCollection.Count:

Ejemplo 1:

// C# code to get the number of strings
// contained in the StringCollection
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // creating a StringCollection named myCol
        StringCollection myCol = new StringCollection();
  
        // creating a string array named myArr
        String[] myArr = new String[] { "First", "Second", "Third",
                                        "Fourth", "Fifth" };
  
        // Copying the elements of a string
        // array to the end of the StringCollection.
        myCol.AddRange(myArr);
  
        // To get number of Strings contained
        // in the StringCollection
        Console.WriteLine("Number of strings in myCol are : " + myCol.Count);
    }
}
Producción:

Number of strings in myCol are : 5

Ejemplo 2:

// C# code to get the number of strings 
// contained in the StringCollection
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // creating a StringCollection named myCol
        StringCollection myCol = new StringCollection();
  
        // creating a string array named myArr
        String[] myArr = new String[] {};
  
        // Copying the elements of a string
        // array to the end of the StringCollection.
        myCol.AddRange(myArr);
  
        // To get number of Strings contained
        // in the StringCollection
        Console.WriteLine("Number of strings in myCol are : " 
                                              + myCol.Count);
    }
}
Producción:

Number of strings in myCol are : 0

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 *