NameValueCollection 类

定义

表示可通过键或索引访问的关联 String 键和 String 值的集合。Represents a collection of associated String keys and String values that can be accessed either with the key or with the index.

public ref class NameValueCollection : System::Collections::Specialized::NameObjectCollectionBase
[System.Serializable]
public class NameValueCollection : System.Collections.Specialized.NameObjectCollectionBase
type NameValueCollection = class
    inherit NameObjectCollectionBase
Public Class NameValueCollection
Inherits NameObjectCollectionBase
继承
NameValueCollection
派生
属性

示例

#using <System.dll>

using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;

void PrintKeysAndValues( NameValueCollection^ myCol );
void PrintKeysAndValues2( NameValueCollection^ myCol );

int main()
{
   // Creates and initializes a new NameValueCollection.
   NameValueCollection^ myCol = gcnew NameValueCollection;
   myCol->Add( "red", "rojo" );
   myCol->Add( "green", "verde" );
   myCol->Add( "blue", "azul" );
   myCol->Add( "red", "rouge" );

   // Displays the values in the NameValueCollection in two different ways.
   Console::WriteLine( "Displays the elements using the AllKeys property and the Item (indexer) property:" );
   PrintKeysAndValues( myCol );
   Console::WriteLine( "Displays the elements using GetKey and Get:" );
   PrintKeysAndValues2( myCol );

   // Gets a value either by index or by key.
   Console::WriteLine( "Index 1 contains the value {0}.", myCol[ 1 ] );
   Console::WriteLine( "Key \"red\" has the value {0}.", myCol[ "red" ] );
   Console::WriteLine();

   // Copies the values to a string array and displays the string array.
   array<String^>^myStrArr = gcnew array<String^>(myCol->Count);
   myCol->CopyTo( myStrArr, 0 );
   Console::WriteLine( "The string array contains:" );
   for each ( String^ s in myStrArr )
      Console::WriteLine( "   {0}", s );
   Console::WriteLine();

   // Searches for a key and deletes it.
   myCol->Remove( "green" );
   Console::WriteLine( "The collection contains the following elements after removing \"green\":" );
   PrintKeysAndValues( myCol );

   // Clears the entire collection.
   myCol->Clear();
   Console::WriteLine( "The collection contains the following elements after it is cleared:" );
   PrintKeysAndValues( myCol );
}

void PrintKeysAndValues( NameValueCollection^ myCol )
{
   Console::WriteLine( "   KEY        VALUE" );
   for each ( String^ s in myCol->AllKeys )
      Console::WriteLine( "   {0,-10} {1}", s, myCol[s] );
   Console::WriteLine();
}

void PrintKeysAndValues2( NameValueCollection^ myCol )
{
   Console::WriteLine( "   [INDEX] KEY        VALUE" );
   for ( int i = 0; i < myCol->Count; i++ )
      Console::WriteLine( "   [{0}]     {1,-10} {2}", i, myCol->GetKey( i ), myCol->Get( i ) );
   Console::WriteLine();
}

/*

This code produces the following output.

Displays the elements using the AllKeys property and the Item (indexer) property:
   KEY        VALUE
   red        rojo,rouge
   green      verde
   blue       azul

Displays the elements using GetKey and Get:
   [INDEX] KEY        VALUE
   [0]     red        rojo,rouge
   [1]     green      verde
   [2]     blue       azul

Index 1 contains the value verde.
Key "red" has the value rojo,rouge.

The string array contains:
   rojo,rouge
   verde
   azul

The collection contains the following elements after removing "green":
   KEY        VALUE
   red        rojo,rouge
   blue       azul

The collection contains the following elements after it is cleared:
   KEY        VALUE


*/
using System;
using System.Collections;
using System.Collections.Specialized;

public class SamplesNameValueCollection  {

   public static void Main()  {

      // Creates and initializes a new NameValueCollection.
      NameValueCollection myCol = new NameValueCollection();
      myCol.Add( "red", "rojo" );
      myCol.Add( "green", "verde" );
      myCol.Add( "blue", "azul" );
      myCol.Add( "red", "rouge" );

      // Displays the values in the NameValueCollection in two different ways.
      Console.WriteLine( "Displays the elements using the AllKeys property and the Item (indexer) property:" );
      PrintKeysAndValues( myCol );
      Console.WriteLine( "Displays the elements using GetKey and Get:" );
      PrintKeysAndValues2( myCol );

      // Gets a value either by index or by key.
      Console.WriteLine( "Index 1 contains the value {0}.", myCol[1] );
      Console.WriteLine( "Key \"red\" has the value {0}.", myCol["red"] );
      Console.WriteLine();

      // Copies the values to a string array and displays the string array.
      String[] myStrArr = new String[myCol.Count];
      myCol.CopyTo( myStrArr, 0 );
      Console.WriteLine( "The string array contains:" );
      foreach ( String s in myStrArr )
         Console.WriteLine( "   {0}", s );
      Console.WriteLine();

      // Searches for a key and deletes it.
      myCol.Remove( "green" );
      Console.WriteLine( "The collection contains the following elements after removing \"green\":" );
      PrintKeysAndValues( myCol );

      // Clears the entire collection.
      myCol.Clear();
      Console.WriteLine( "The collection contains the following elements after it is cleared:" );
      PrintKeysAndValues( myCol );

   }

   public static void PrintKeysAndValues( NameValueCollection myCol )  {
      Console.WriteLine( "   KEY        VALUE" );
      foreach ( String s in myCol.AllKeys )
         Console.WriteLine( "   {0,-10} {1}", s, myCol[s] );
      Console.WriteLine();
   }

   public static void PrintKeysAndValues2( NameValueCollection myCol )  {
      Console.WriteLine( "   [INDEX] KEY        VALUE" );
      for ( int i = 0; i < myCol.Count; i++ )
         Console.WriteLine( "   [{0}]     {1,-10} {2}", i, myCol.GetKey(i), myCol.Get(i) );
      Console.WriteLine();
   }

}

/*

This code produces the following output.

Displays the elements using the AllKeys property and the Item (indexer) property:
   KEY        VALUE
   red        rojo,rouge
   green      verde
   blue       azul

Displays the elements using GetKey and Get:
   [INDEX] KEY        VALUE
   [0]     red        rojo,rouge
   [1]     green      verde
   [2]     blue       azul

Index 1 contains the value verde.
Key "red" has the value rojo,rouge.

The string array contains:
   rojo,rouge
   verde
   azul

The collection contains the following elements after removing "green":
   KEY        VALUE
   red        rojo,rouge
   blue       azul

The collection contains the following elements after it is cleared:
   KEY        VALUE


*/
' The following code example demonstrates several of the properties and methods of ListDictionary.

Imports System.Collections
Imports System.Collections.Specialized


Public Class SamplesNameValueCollection

    Public Shared Sub Main()

        ' Creates and initializes a new NameValueCollection.
        Dim myCol As New NameValueCollection()
        myCol.Add("red", "rojo")
        myCol.Add("green", "verde")
        myCol.Add("blue", "azul")
        myCol.Add("red", "rouge")

        ' Displays the values in the NameValueCollection in two different ways.
        Console.WriteLine("Displays the elements using the AllKeys property and the Item (indexer) property:")
        PrintKeysAndValues(myCol)
        Console.WriteLine("Displays the elements using GetKey and Get:")
        PrintKeysAndValues2(myCol)

        ' Gets a value either by index or by key.
        Console.WriteLine("Index 1 contains the value {0}.", myCol(1))
        Console.WriteLine("Key ""red"" has the value {0}.", myCol("red"))
        Console.WriteLine()

        ' Copies the values to a string array and displays the string array.
        Dim myStrArr(myCol.Count) As String
        myCol.CopyTo(myStrArr, 0)
        Console.WriteLine("The string array contains:")
        Dim s As String
        For Each s In myStrArr
            Console.WriteLine("   {0}", s)
        Next s
        Console.WriteLine()

        ' Searches for a key and deletes it.
        myCol.Remove("green")
        Console.WriteLine("The collection contains the following elements after removing ""green"":")
        PrintKeysAndValues(myCol)

        ' Clears the entire collection.
        myCol.Clear()
        Console.WriteLine("The collection contains the following elements after it is cleared:")
        PrintKeysAndValues(myCol)

    End Sub

    Public Shared Sub PrintKeysAndValues(myCol As NameValueCollection)
        Console.WriteLine("   KEY        VALUE")
        Dim s As String
        For Each s In  myCol.AllKeys
            Console.WriteLine("   {0,-10} {1}", s, myCol(s))
        Next s
        Console.WriteLine()
    End Sub

    Public Shared Sub PrintKeysAndValues2(myCol As NameValueCollection)
        Console.WriteLine("   [INDEX] KEY        VALUE")
        Dim i As Integer
        For i = 0 To myCol.Count - 1
            Console.WriteLine("   [{0}]     {1,-10} {2}", i, myCol.GetKey(i), myCol.Get(i))
        Next i
        Console.WriteLine()
    End Sub

End Class


'This code produces the following output.
'
'Displays the elements using the AllKeys property and the Item (indexer) property:
'   KEY        VALUE
'   red        rojo,rouge
'   green      verde
'   blue       azul
'
'Displays the elements using GetKey and Get:
'   [INDEX] KEY        VALUE
'   [0]     red        rojo,rouge
'   [1]     green      verde
'   [2]     blue       azul
'
'Index 1 contains the value verde.
'Key "red" has the value rojo,rouge.
'
'The string array contains:
'   red
'   green
'   blue
'
'
'The collection contains the following elements after removing "green":
'   KEY        VALUE
'   red        rojo,rouge
'   blue       azul
'
'The collection contains the following elements after it is cleared:
'   KEY        VALUE
'
'

注解

此集合基于 NameObjectCollectionBase 类。This collection is based on the NameObjectCollectionBase class. 集合中的每个元素都是一个键/值对。Each element of the collection is a key/value pair. 但是,与 NameObjectCollectionBase不同,此类可以将多个字符串值存储在一个键下。However, unlike the NameObjectCollectionBase, this class can store multiple string values under a single key.

此类可用于标头、查询字符串和表单数据。This class can be used for headers, query strings and form data.

此类型的集合不保留元素的顺序,枚举集合时不保证特定顺序。Collections of this type do not preserve the ordering of elements, and no particular ordering is guaranteed when enumerating the collection.

NameValueCollection 的容量是 NameValueCollection 可以容纳的元素数。The capacity of a NameValueCollection is the number of elements the NameValueCollection can hold. 添加元素时,其容量将根据需要通过重新分配自动增加。As elements are added, its capacity is automatically increased as required through reallocation.

哈希代码提供程序在 NameValueCollection中的密钥的哈希代码密密麻麻。The hash code provider dispenses hash codes for keys in the NameValueCollection. 默认哈希代码提供程序是 CaseInsensitiveHashCodeProviderThe default hash code provider is the CaseInsensitiveHashCodeProvider.

比较器确定两个键是否相等。The comparer determines whether two keys are equal. 默认比较器是使用固定区域性的约定的 CaseInsensitiveComparer;也就是说,默认情况下,键比较不区分大小写。The default comparer is a CaseInsensitiveComparer that uses the conventions of the invariant culture; that is, key comparisons are case-insensitive by default. 若要执行区分大小写的键比较,请调用 NameValueCollection.NameValueCollection(IEqualityComparer) 构造函数,并将 StringComparer.CurrentCultureStringComparer.InvariantCultureStringComparer.Ordinal 值作为 equalityComparer 参数提供。To perform case-sensitive key comparisons, call the NameValueCollection.NameValueCollection(IEqualityComparer) constructor, and provide a value of StringComparer.CurrentCulture, StringComparer.InvariantCulture, or StringComparer.Ordinal as the equalityComparer argument. 有关区域性如何影响比较和排序的详细信息,请参阅执行不区分区域性的字符串操作For more information about how culture affects comparisons and sorting, see Performing Culture-Insensitive String Operations.

null 允许作为键或值。null is allowed as a key or as a value.

注意

Get 方法不区分 null,因为找不到指定的键并返回 null,因为与该键关联的值是 null的。The Get method does not distinguish between null which is returned because the specified key is not found and null which is returned because the value associated with the key is null.

构造函数

NameValueCollection()

初始化 NameValueCollection 类的新实例,该实例为空且具有默认初始容量,并使用不区分大小写的默认哈希代码提供程序和不区分大小写的默认比较器。Initializes a new instance of the NameValueCollection class that is empty, has the default initial capacity and uses the default case-insensitive hash code provider and the default case-insensitive comparer.

NameValueCollection(IEqualityComparer)

初始化 NameValueCollection 类的新实例,该实例为空、具有默认的初始容量并使用指定的 IEqualityComparer 对象。Initializes a new instance of the NameValueCollection class that is empty, has the default initial capacity, and uses the specified IEqualityComparer object.

NameValueCollection(IHashCodeProvider, IComparer)

初始化 NameValueCollection 类的新实例,该实例为空且具有默认初始容量,并使用指定的哈希代码提供程序和指定的比较器。Initializes a new instance of the NameValueCollection class that is empty, has the default initial capacity and uses the specified hash code provider and the specified comparer.

NameValueCollection(Int32)

初始化 NameValueCollection 类的新实例,该实例为空且具有指定的初始容量,并使用不区分大小写的默认哈希代码提供程序和不区分大小写的默认比较器。Initializes a new instance of the NameValueCollection class that is empty, has the specified initial capacity and uses the default case-insensitive hash code provider and the default case-insensitive comparer.

NameValueCollection(Int32, IEqualityComparer)

初始化 NameValueCollection 类的新实例,该实例为空、具有指定的初始容量并使用指定的 IEqualityComparer 对象。Initializes a new instance of the NameValueCollection class that is empty, has the specified initial capacity, and uses the specified IEqualityComparer object.

NameValueCollection(Int32, IHashCodeProvider, IComparer)

初始化 NameValueCollection 类的新实例,该实例为空且具有指定的初始容量,并使用指定的哈希代码提供程序和指定的比较器。Initializes a new instance of the NameValueCollection class that is empty, has the specified initial capacity and uses the specified hash code provider and the specified comparer.

NameValueCollection(Int32, NameValueCollection)

将项从指定的 NameValueCollection 复制到一个新的 NameValueCollection,这个新集合使用指定的初始容量或与具有与复制的项数相等的初始容量(两者中较大的一个),并使用不区分大小写的默认哈希代码提供程序和不区分大小写的默认比较器。Copies the entries from the specified NameValueCollection to a new NameValueCollection with the specified initial capacity or the same initial capacity as the number of entries copied, whichever is greater, and using the default case-insensitive hash code provider and the default case-insensitive comparer.

NameValueCollection(NameValueCollection)

将项从指定的 NameValueCollection 复制到一个新的 NameValueCollection,这个新集合的初始容量与复制的项数相等,并使用与源集合相同的哈希代码提供程序和比较器。Copies the entries from the specified NameValueCollection to a new NameValueCollection with the same initial capacity as the number of entries copied and using the same hash code provider and the same comparer as the source collection.

NameValueCollection(SerializationInfo, StreamingContext)

初始化 NameValueCollection 类的新实例,该实例可序列化且使用指定的 SerializationInfoStreamingContextInitializes a new instance of the NameValueCollection class that is serializable and uses the specified SerializationInfo and StreamingContext.

属性

AllKeys

获取 NameValueCollection 中的所有键。Gets all the keys in the NameValueCollection.

Count

获取包含在 NameObjectCollectionBase 实例中的键/值对的数目。Gets the number of key/value pairs contained in the NameObjectCollectionBase instance.

(继承自 NameObjectCollectionBase)
IsReadOnly

获取或设置一个值,通过该值指示 NameObjectCollectionBase 实例是否为只读的。Gets or sets a value indicating whether the NameObjectCollectionBase instance is read-only.

(继承自 NameObjectCollectionBase)
Item[Int32]

获取 NameValueCollection 中指定索引处的项。Gets the entry at the specified index of the NameValueCollection.

Item[String]

获取或设置 NameValueCollection 中具有指定键的项。Gets or sets the entry with the specified key in the NameValueCollection.

Keys

获取 NameObjectCollectionBase.KeysCollection 实例,该实例包含 NameObjectCollectionBase 实例中的所有键。Gets a NameObjectCollectionBase.KeysCollection instance that contains all the keys in the NameObjectCollectionBase instance.

(继承自 NameObjectCollectionBase)

方法

Add(NameValueCollection)

将指定 NameValueCollection 中的项复制到当前 NameValueCollectionCopies the entries in the specified NameValueCollection to the current NameValueCollection.

Add(String, String)

将具有指定名称和值的项添加到 NameValueCollectionAdds an entry with the specified name and value to the NameValueCollection.

BaseAdd(String, Object)

将具有指定键和值的项添加到 NameObjectCollectionBase 实例中。Adds an entry with the specified key and value into the NameObjectCollectionBase instance.

(继承自 NameObjectCollectionBase)
BaseClear()

移除 NameObjectCollectionBase 实例中的所有项。Removes all entries from the NameObjectCollectionBase instance.

(继承自 NameObjectCollectionBase)
BaseGet(Int32)

获取 NameObjectCollectionBase 实例的指定索引处的项值。Gets the value of the entry at the specified index of the NameObjectCollectionBase instance.

(继承自 NameObjectCollectionBase)
BaseGet(String)

获取 NameObjectCollectionBase 实例中第一个具有指定键的项值。Gets the value of the first entry with the specified key from the NameObjectCollectionBase instance.

(继承自 NameObjectCollectionBase)
BaseGetAllKeys()

返回 String 数组,该数组包含 NameObjectCollectionBase 实例中的所有键。Returns a String array that contains all the keys in the NameObjectCollectionBase instance.

(继承自 NameObjectCollectionBase)
BaseGetAllValues()

返回 Object 数组,该数组包含 NameObjectCollectionBase 实例中的所有值。Returns an Object array that contains all the values in the NameObjectCollectionBase instance.

(继承自 NameObjectCollectionBase)
BaseGetAllValues(Type)

返回指定类型的数组,该数组包含 NameObjectCollectionBase 实例中的所有值。Returns an array of the specified type that contains all the values in the NameObjectCollectionBase instance.

(继承自 NameObjectCollectionBase)
BaseGetKey(Int32)

获取 NameObjectCollectionBase 实例的指定索引处的项键。Gets the key of the entry at the specified index of the NameObjectCollectionBase instance.

(继承自 NameObjectCollectionBase)
BaseHasKeys()

获取一个值,通过该值指示 NameObjectCollectionBase 实例是否包含键不为 null 的项。Gets a value indicating whether the NameObjectCollectionBase instance contains entries whose keys are not null.

(继承自 NameObjectCollectionBase)
BaseRemove(String)

移除 NameObjectCollectionBase 实例中具有指定键的项。Removes the entries with the specified key from the NameObjectCollectionBase instance.

(继承自 NameObjectCollectionBase)
BaseRemoveAt(Int32)

移除 NameObjectCollectionBase 实例的指定索引处的项。Removes the entry at the specified index of the NameObjectCollectionBase instance.

(继承自 NameObjectCollectionBase)
BaseSet(Int32, Object)

设置 NameObjectCollectionBase 实例的指定索引处的项值。Sets the value of the entry at the specified index of the NameObjectCollectionBase instance.

(继承自 NameObjectCollectionBase)
BaseSet(String, Object)

NameObjectCollectionBase 实例中第一个具有指定键的项设置值(如果有这样的项);否则将具有指定键和值的项添加到 NameObjectCollectionBase 实例中。Sets the value of the first entry with the specified key in the NameObjectCollectionBase instance, if found; otherwise, adds an entry with the specified key and value into the NameObjectCollectionBase instance.

(继承自 NameObjectCollectionBase)
Clear()

使缓存数组无效,并将所有项从 NameValueCollection 中移除。Invalidates the cached arrays and removes all entries from the NameValueCollection.

CopyTo(Array, Int32)

从目标数组的指定索引处开始将整个 NameValueCollection 复制到兼容的一维 ArrayCopies the entire NameValueCollection to a compatible one-dimensional Array, starting at the specified index of the target array.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
Get(Int32)

获取 NameValueCollection 中指定索引处的值,这些值已合并为一个以逗号分隔的列表。Gets the values at the specified index of the NameValueCollection combined into one comma-separated list.

Get(String)

获取与 NameValueCollection 中的指定键关联的值,这些值已合并为一个以逗号分隔的列表。Gets the values associated with the specified key from the NameValueCollection combined into one comma-separated list.

GetEnumerator()

返回循环访问 NameObjectCollectionBase 的枚举器。Returns an enumerator that iterates through the NameObjectCollectionBase.

(继承自 NameObjectCollectionBase)
GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetKey(Int32)

获取 NameValueCollection 的指定索引处的键。Gets the key at the specified index of the NameValueCollection.

GetObjectData(SerializationInfo, StreamingContext)

实现 ISerializable 接口,并返回序列化 NameObjectCollectionBase 实例所需的数据。Implements the ISerializable interface and returns the data needed to serialize the NameObjectCollectionBase instance.

(继承自 NameObjectCollectionBase)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
GetValues(Int32)

获取 NameValueCollection 中指定索引处的值。Gets the values at the specified index of the NameValueCollection.

GetValues(String)

获取与 NameValueCollection 中的指定键关联的值。Gets the values associated with the specified key from the NameValueCollection.

HasKeys()

获取一个值,该值指示 NameValueCollection 是否包含非 null 的键。Gets a value indicating whether the NameValueCollection contains keys that are not null.

InvalidateCachedArrays()

将集合的缓存数组重置为 nullResets the cached arrays of the collection to null.

MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
OnDeserialization(Object)

实现 ISerializable 接口,并在完成反序列化之后引发反序列化事件。Implements the ISerializable interface and raises the deserialization event when the deserialization is complete.

(继承自 NameObjectCollectionBase)
Remove(String)

移除 NameObjectCollectionBase 实例中具有指定键的项。Removes the entries with the specified key from the NameObjectCollectionBase instance.

Set(String, String)

设置 NameValueCollection 中某个项的值。Sets the value of an entry in the NameValueCollection.

ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

显式接口实现

ICollection.CopyTo(Array, Int32)

从目标数组的指定索引处开始将整个 NameObjectCollectionBase 复制到兼容的一维 ArrayCopies the entire NameObjectCollectionBase to a compatible one-dimensional Array, starting at the specified index of the target array.

(继承自 NameObjectCollectionBase)
ICollection.IsSynchronized

获取一个值,该值指示对 NameObjectCollectionBase 对象的访问是否同步(线程安全)。Gets a value indicating whether access to the NameObjectCollectionBase object is synchronized (thread safe).

(继承自 NameObjectCollectionBase)
ICollection.SyncRoot

获取一个对象,该对象可用于同步对 NameObjectCollectionBase 对象的访问。Gets an object that can be used to synchronize access to the NameObjectCollectionBase object.

(继承自 NameObjectCollectionBase)

扩展方法

Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

启用查询的并行化。Enables parallelization of a query.

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryableConverts an IEnumerable to an IQueryable.

适用于

线程安全性

此类型的公共静态(Shared Visual Basic)成员是线程安全的。Public static (Shared in Visual Basic) members of this type are thread safe. 但不保证所有实例成员都是线程安全的。Any instance members are not guaranteed to be thread safe.

此实现不为 NameValueCollection提供同步(线程安全)包装器,但派生类可以使用 NameObjectCollectionBase 类的 SyncRoot 属性创建 NameValueCollection 的同步版本。This implementation does not provide a synchronized (thread safe) wrapper for a NameValueCollection, but derived classes can create their own synchronized versions of the NameValueCollection using the SyncRoot property of the NameObjectCollectionBase class.

枚举集合本质上不是线程安全的过程。Enumerating through a collection is intrinsically not a thread safe procedure. 即使某个集合已同步,其他线程仍可以修改该集合,这会导致枚举数引发异常。Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. 若要确保枚举过程中的线程安全性,可以在整个枚举期间锁定集合,或者捕获由其他线程进行的更改所导致的异常。To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.

另请参阅