📌  相关文章
📜  如何在C#中创建StringCollection

📅  最后修改于: 2021-05-29 15:55:12             🧑  作者: Mango

StringCollection()构造函数用于初始化StringCollection类的新实例。 StringCollection类是.NET Framework类库的新增内容,它代表字符串的集合。 StringCollection类在System.Collections.Specialized命名空间中定义。

句法:

public StringCollection ();

范例1:

// C# Program to illustrate how
// to create a StringCollection
using System;
using System.Collections;
using System.Collections.Specialized;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // sc is the StringCollection object
        // StringCollection() is the constructor
        // used to initializes a new
        // instance of the StringCollection class
        StringCollection sc = new StringCollection();
  
        // Count property is used to get the
        // number of elements in StringCollection
        // It will give 0 as no elements
        // are present currently
        Console.WriteLine("Number of elements: "+
                                      sc.Count);
    }
}
输出:
Number of elements: 0

范例2:

// C# Program to illustrate how
// to create a StringCollection
using System;
using System.Collections;
using System.Collections.Specialized;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // sc is the StringCollection object
        // StringCollection() is the constructor
        // used to initializes a new
        // instance of the StringCollection class
        StringCollection sc = new StringCollection();
  
        Console.Write("Before Add Method: ");
  
        // Count property is used to get the
        // number of elements in StringCollection
        // It will give 0 as no elements
        // are present currently
        Console.WriteLine(sc.Count);
  
        Console.Write("After Add Method: ");
  
        // Adding elements in StringCollection
        sc.Add("A");
        sc.Add("B");
        sc.Add("C");
        sc.Add("D");
        sc.Add("E");
  
        // Count property is used to get the
        // number of elements in ld
        Console.WriteLine(sc.Count);
    }
}
输出:
Before Add Method: 0
After Add Method: 5

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.specialized.stringcollection.-ctor?view=netframework-4.7.2