StringCollection 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示字符串的集合。
public ref class StringCollection : System::Collections::IList
public class StringCollection : System.Collections.IList
[System.Serializable]
public class StringCollection : System.Collections.IList
type StringCollection = class
interface ICollection
interface IEnumerable
interface IList
[<System.Serializable>]
type StringCollection = class
interface IList
interface ICollection
interface IEnumerable
Public Class StringCollection
Implements IList
- 继承
-
StringCollection
- 派生
- 属性
- 实现
示例
下面的代码示例演示了多个属性和方法 StringCollection。
using System;
using System.Collections;
using System.Collections.Specialized;
public class SamplesStringCollection {
public static void Main() {
// Create and initializes a new StringCollection.
StringCollection myCol = new StringCollection();
// Add a range of elements from an array to the end of the StringCollection.
String[] myArr = new String[] { "RED", "orange", "yellow", "RED", "green", "blue", "RED", "indigo", "violet", "RED" };
myCol.AddRange( myArr );
// Display the contents of the collection using foreach. This is the preferred method.
Console.WriteLine( "Displays the elements using foreach:" );
PrintValues1( myCol );
// Display the contents of the collection using the enumerator.
Console.WriteLine( "Displays the elements using the IEnumerator:" );
PrintValues2( myCol );
// Display the contents of the collection using the Count and Item properties.
Console.WriteLine( "Displays the elements using the Count and Item properties:" );
PrintValues3( myCol );
// Add one element to the end of the StringCollection and insert another at index 3.
myCol.Add( "* white" );
myCol.Insert( 3, "* gray" );
Console.WriteLine( "After adding \"* white\" to the end and inserting \"* gray\" at index 3:" );
PrintValues1( myCol );
// Remove one element from the StringCollection.
myCol.Remove( "yellow" );
Console.WriteLine( "After removing \"yellow\":" );
PrintValues1( myCol );
// Remove all occurrences of a value from the StringCollection.
int i = myCol.IndexOf( "RED" );
while ( i > -1 ) {
myCol.RemoveAt( i );
i = myCol.IndexOf( "RED" );
}
// Verify that all occurrences of "RED" are gone.
if ( myCol.Contains( "RED" ) )
Console.WriteLine( "*** The collection still contains \"RED\"." );
Console.WriteLine( "After removing all occurrences of \"RED\":" );
PrintValues1( myCol );
// Copy the collection to a new array starting at index 0.
String[] myArr2 = new String[myCol.Count];
myCol.CopyTo( myArr2, 0 );
Console.WriteLine( "The new array contains:" );
for ( i = 0; i < myArr2.Length; i++ ) {
Console.WriteLine( " [{0}] {1}", i, myArr2[i] );
}
Console.WriteLine();
// Clears the entire collection.
myCol.Clear();
Console.WriteLine( "After clearing the collection:" );
PrintValues1( myCol );
}
// Uses the foreach statement which hides the complexity of the enumerator.
// NOTE: The foreach statement is the preferred way of enumerating the contents of a collection.
public static void PrintValues1( StringCollection myCol ) {
foreach ( Object obj in myCol )
Console.WriteLine( " {0}", obj );
Console.WriteLine();
}
// Uses the enumerator.
// NOTE: The foreach statement is the preferred way of enumerating the contents of a collection.
public static void PrintValues2( StringCollection myCol ) {
StringEnumerator myEnumerator = myCol.GetEnumerator();
while ( myEnumerator.MoveNext() )
Console.WriteLine( " {0}", myEnumerator.Current );
Console.WriteLine();
}
// Uses the Count and Item properties.
public static void PrintValues3( StringCollection myCol ) {
for ( int i = 0; i < myCol.Count; i++ )
Console.WriteLine( " {0}", myCol[i] );
Console.WriteLine();
}
}
/*
This code produces the following output.
Displays the elements using foreach:
RED
orange
yellow
RED
green
blue
RED
indigo
violet
RED
Displays the elements using the IEnumerator:
RED
orange
yellow
RED
green
blue
RED
indigo
violet
RED
Displays the elements using the Count and Item properties:
RED
orange
yellow
RED
green
blue
RED
indigo
violet
RED
After adding "* white" to the end and inserting "* gray" at index 3:
RED
orange
yellow
* gray
RED
green
blue
RED
indigo
violet
RED
* white
After removing "yellow":
RED
orange
* gray
RED
green
blue
RED
indigo
violet
RED
* white
After removing all occurrences of "RED":
orange
* gray
green
blue
indigo
violet
* white
The new array contains:
[0] orange
[1] * gray
[2] green
[3] blue
[4] indigo
[5] violet
[6] * white
After clearing the collection:
*/
Imports System.Collections
Imports System.Collections.Specialized
Public Class SamplesStringCollection
Public Shared Sub Main()
' Create and initializes a new StringCollection.
Dim myCol As New StringCollection()
' Add a range of elements from an array to the end of the StringCollection.
Dim myArr() As String = {"RED", "orange", "yellow", "RED", "green", "blue", "RED", "indigo", "violet", "RED"}
myCol.AddRange(myArr)
' Display the contents of the collection using foreach. This is the preferred method.
Console.WriteLine("Displays the elements using foreach:")
PrintValues1(myCol)
' Display the contents of the collection using the enumerator.
Console.WriteLine("Displays the elements using the IEnumerator:")
PrintValues2(myCol)
' Display the contents of the collection using the Count and Item properties.
Console.WriteLine("Displays the elements using the Count and Item properties:")
PrintValues3(myCol)
' Add one element to the end of the StringCollection and insert another at index 3.
myCol.Add("* white")
myCol.Insert(3, "* gray")
Console.WriteLine("After adding ""* white"" to the end and inserting ""* gray"" at index 3:")
PrintValues1(myCol)
' Remove one element from the StringCollection.
myCol.Remove("yellow")
Console.WriteLine("After removing ""yellow"":")
PrintValues1(myCol)
' Remove all occurrences of a value from the StringCollection.
Dim i As Integer = myCol.IndexOf("RED")
While i > - 1
myCol.RemoveAt(i)
i = myCol.IndexOf("RED")
End While
' Verify that all occurrences of "RED" are gone.
If myCol.Contains("RED") Then
Console.WriteLine("*** The collection still contains ""RED"".")
End If
Console.WriteLine("After removing all occurrences of ""RED"":")
PrintValues1(myCol)
' Copy the collection to a new array starting at index 0.
Dim myArr2(myCol.Count) As String
myCol.CopyTo(myArr2, 0)
Console.WriteLine("The new array contains:")
For i = 0 To myArr2.Length - 1
Console.WriteLine(" [{0}] {1}", i, myArr2(i))
Next i
Console.WriteLine()
' Clears the entire collection.
myCol.Clear()
Console.WriteLine("After clearing the collection:")
PrintValues1(myCol)
End Sub
' Uses the foreach statement which hides the complexity of the enumerator.
' NOTE: The foreach statement is the preferred way of enumerating the contents of a collection.
Public Shared Sub PrintValues1(myCol As StringCollection)
Dim obj As [Object]
For Each obj In myCol
Console.WriteLine(" {0}", obj)
Next obj
Console.WriteLine()
End Sub
' Uses the enumerator.
' NOTE: The foreach statement is the preferred way of enumerating the contents of a collection.
Public Shared Sub PrintValues2(myCol As StringCollection)
Dim myEnumerator As StringEnumerator = myCol.GetEnumerator()
While myEnumerator.MoveNext()
Console.WriteLine(" {0}", myEnumerator.Current)
End While
Console.WriteLine()
End Sub
' Uses the Count and Item properties.
Public Shared Sub PrintValues3(myCol As StringCollection)
Dim i As Integer
For i = 0 To myCol.Count - 1
Console.WriteLine(" {0}", myCol(i))
Next i
Console.WriteLine()
End Sub
End Class
'This code produces the following output.
'
'Displays the elements using foreach:
' RED
' orange
' yellow
' RED
' green
' blue
' RED
' indigo
' violet
' RED
'
'Displays the elements using the IEnumerator:
' RED
' orange
' yellow
' RED
' green
' blue
' RED
' indigo
' violet
' RED
'
'Displays the elements using the Count and Item properties:
' RED
' orange
' yellow
' RED
' green
' blue
' RED
' indigo
' violet
' RED
'
'After adding "* white" to the end and inserting "* gray" at index 3:
' RED
' orange
' yellow
' * gray
' RED
' green
' blue
' RED
' indigo
' violet
' RED
' * white
'
'After removing "yellow":
' RED
' orange
' * gray
' RED
' green
' blue
' RED
' indigo
' violet
' RED
' * white
'
'After removing all occurrences of "RED":
' orange
' * gray
' green
' blue
' indigo
' violet
' * white
'
'The new array contains:
' [0] orange
' [1] * gray
' [2] green
' [3] blue
' [4] indigo
' [5] violet
' [6] * white
'
'After clearing the collection:
'
注解
StringCollection 接受 null 为有效值并允许重复元素。
字符串比较区分大小写。
可以使用整数索引访问此集合中的元素。 此集合中的索引从零开始。
构造函数
| 名称 | 说明 |
|---|---|
| StringCollection() |
初始化 StringCollection 类的新实例。 |
属性
| 名称 | 说明 |
|---|---|
| Count |
获取包含在 .. 中的 StringCollection字符串数。 |
| IsReadOnly |
获取一个值,该值指示是否 StringCollection 为只读。 |
| IsSynchronized |
获取一个值,该值指示是否同步对 StringCollection 的访问(线程安全)。 |
| Item[Int32] |
获取或设置指定索引处的元素。 |
| SyncRoot |
获取可用于同步对 . StringCollection的访问的对象。 |
方法
| 名称 | 说明 |
|---|---|
| Add(String) |
将字符串添加到该 StringCollection字符串的末尾。 |
| AddRange(String[]) |
将字符串数组的元素复制到字符串 StringCollection数组的末尾。 |
| Clear() |
从 .中删除所有字符串 StringCollection。 |
| Contains(String) |
确定指定的字符串是否在 . 中 StringCollection。 |
| CopyTo(String[], Int32) |
从目标数组的指定索引处开始,将整个 StringCollection 值复制到字符串的一维数组。 |
| Equals(Object) |
确定指定的对象是否等于当前对象。 (继承自 Object) |
| GetEnumerator() |
返回循环访问的StringCollection一个 StringEnumerator 。 |
| GetHashCode() |
用作默认哈希函数。 (继承自 Object) |
| GetType() |
获取当前实例的 Type。 (继承自 Object) |
| IndexOf(String) |
搜索指定的字符串并返回第一个匹配项的 StringCollection从零开始的索引。 |
| Insert(Int32, String) |
将字符串插入 StringCollection 指定索引处。 |
| MemberwiseClone() |
创建当前 Object的浅表副本。 (继承自 Object) |
| Remove(String) |
从中 StringCollection删除特定字符串的第一个匹配项。 |
| RemoveAt(Int32) |
删除指定索引处的 StringCollection字符串。 |
| ToString() |
返回一个表示当前对象的字符串。 (继承自 Object) |
显式接口实现
| 名称 | 说明 |
|---|---|
| ICollection.CopyTo(Array, Int32) |
将整个 StringCollection 复制到兼容的一维 Array,从目标数组的指定索引处开始。 |
| IEnumerable.GetEnumerator() |
返回循环访问的StringCollection一个 IEnumerator 。 |
| IList.Add(Object) |
将对象添加到该 StringCollection对象的末尾。 |
| IList.Contains(Object) |
确定元素是否在 StringCollection. |
| IList.IndexOf(Object) |
搜索指定的 Object 并返回整个 StringCollection匹配项中第一个匹配项的从零开始的索引。 |
| IList.Insert(Int32, Object) |
将元素 StringCollection 插入指定索引处。 |
| IList.IsFixedSize |
获取一个值,该值指示对象是否 StringCollection 具有固定大小。 |
| IList.IsReadOnly |
获取一个值,该值指示对象 StringCollection 是否为只读。 |
| IList.Item[Int32] |
获取或设置指定索引处的元素。 |
| IList.Remove(Object) |
从中 StringCollection删除特定对象的第一个匹配项。 |
扩展方法
| 名称 | 说明 |
|---|---|
| AsParallel(IEnumerable) |
启用查询的并行化。 |
| AsQueryable(IEnumerable) |
将 IEnumerable 转换为 IQueryable。 |
| Cast<TResult>(IEnumerable) |
将 IEnumerable 的元素强制转换为指定类型。 |
| OfType<TResult>(IEnumerable) |
根据指定类型筛选 IEnumerable 的元素。 |
适用于
线程安全性
此类型的公共静态(Shared 在 Visual Basic 中)成员是线程安全的。 不能保证任何实例成员是线程安全的。
此实现不提供同步的(线程安全)包装器StringCollection,但派生类可以创建其自己的使用属性的StringCollectionSyncRoot同步版本。
通过集合进行枚举本质上不是线程安全过程。 即使集合同步,其他线程仍可以修改集合,这会导致枚举器引发异常。 若要保证枚举期间的线程安全性,可以在整个枚举期间锁定集合,也可以捕获由其他线程所做的更改导致的异常。