System.Collections.Specialized.NameValueCollection Class

Assembly: System.dll
Namespace: System.Collections.Specialized
Summary
Represents a sorted collection of associated String keys and String values that can be accessed either with the key or with the index.
C# Syntax:
[Serializable]
public class NameValueCollection : NameObjectCollectionBase
Thread Safety
Public static (non-instance) members of this type are safe for multithreaded operations. Instance members are not guaranteed to be thread-safe.

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 NameObjectCollectionBase.System.Collections.ICollection.SyncRoot property.

Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could 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.

Remarks
This collection is based on the NameObjectCollectionBase class. However, unlike the NameObjectCollectionBase, this class stores multiple string values under a single key.

This class can be used for headers, query strings and form data.

The capacity is the number of key-and-value pairs that the NameValueCollection can contain. The default initial capacity is zero. The capacity is automatically increased as required.

The hash code provider dispenses hash codes for keys in the NameValueCollection. The default hash code provider is the CaseInsensitiveHashCodeProvider.

The comparer determines whether two keys are equal. The default comparer is the CaseInsensitiveComparer.

See also:
System.Collections.Specialized Namespace | NameObjectCollectionBase

System.Collections.Specialized.NameValueCollection Member List:

Public Constructors
ctor #1 Overloaded:
.ctor()

Default constructor. This constructor is called by derived class constructors to initialize state in this type.
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.
ctor #2 Overloaded:
.ctor(int capacity)

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.
ctor #3 Overloaded:
.ctor(NameValueCollection col)

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.
ctor #4 Overloaded:
.ctor(IHashCodeProvider hashProvider, IComparer comparer)

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.
ctor #5 Overloaded:
.ctor(int capacity, NameValueCollection col)

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.
ctor #7 Overloaded:
.ctor(int capacity, IHashCodeProvider hashProvider, IComparer comparer)

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.
Public Properties
AllKeys Read-only

Gets all the keys in the NameValueCollection.
Count
(inherited from System.Collections.Specialized.NameObjectCollectionBase)
Read-only

See base class member description: System.Collections.Specialized.NameObjectCollectionBase.Count


Gets the number of key-and-value pairs contained in the NameObjectCollectionBase instance.
Item Read-only

Overloaded:
Item[int index] {get

Gets the entry at the specified index of the NameValueCollection.
Item Read-write

Overloaded:
Item[string name] {get

Gets or sets the entry with the specified key in the NameValueCollection.
Keys
(inherited from System.Collections.Specialized.NameObjectCollectionBase)
Read-only

See base class member description: System.Collections.Specialized.NameObjectCollectionBase.Keys


Gets a NameObjectCollectionBase.KeysCollection instance that contains all the keys in the NameObjectCollectionBase instance.
Public Methods
Add Overloaded:
Add(NameValueCollection c)

Copies the entries in the specified NameValueCollection to the current NameValueCollection.
Add Overloaded:
Add(string name, string value)

Adds an entry with the specified name and value to the NameValueCollection.
Clear Invalidates the cached arrays and removes all entries from the NameValueCollection.
CopyTo Copies the entire NameValueCollection to a compatible one-dimensional Array, starting at the specified index of the target array.
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
Get Overloaded:
Get(int index)

Gets the values at the specified index of the NameValueCollection combined into one comma-separated list.
Get Overloaded:
Get(string name)

Gets the values associated with the specified key from the NameValueCollection combined into one comma-separated list.
GetEnumerator
(inherited from System.Collections.Specialized.NameObjectCollectionBase)
See base class member description: System.Collections.Specialized.NameObjectCollectionBase.GetEnumerator


Returns an enumerator that can iterate through the NameObjectCollectionBase.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetKey Gets the key at the specified index of the NameValueCollection.
GetObjectData
(inherited from System.Collections.Specialized.NameObjectCollectionBase)
See base class member description: System.Collections.Specialized.NameObjectCollectionBase.GetObjectData


Implements the ISerializable interface and returns the data needed to serialize the NameObjectCollectionBase instance.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
GetValues Overloaded:
GetValues(int index)

Gets the values at the specified index of the NameValueCollection.
GetValues Overloaded:
GetValues(string name)

Gets the values associated with the specified key from the NameValueCollection.
HasKeys Gets a value indicating whether the NameValueCollection contains keys that are not null.
OnDeserialization
(inherited from System.Collections.Specialized.NameObjectCollectionBase)
See base class member description: System.Collections.Specialized.NameObjectCollectionBase.OnDeserialization


Implements the ISerializable interface and raises the deserialization event when the deserialization is complete.
Remove Removes the entries with the specified key from the NameObjectCollectionBase instance.
Set Sets the value of an entry in the NameValueCollection.
ToString
(inherited from System.Object)
See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
Protected Constructors
ctor #6 Overloaded:
.ctor(SerializationInfo info, StreamingContext context)

Initializes a new instance of the NameValueCollection class that is serializable and uses the specified SerializationInfo and StreamingContext.
Protected Properties
IsReadOnly
(inherited from System.Collections.Specialized.NameObjectCollectionBase)
Read-write

See base class member description: System.Collections.Specialized.NameObjectCollectionBase.IsReadOnly


Gets or sets a value indicating whether the NameObjectCollectionBase instance is read-only.
Protected Methods
BaseAdd
(inherited from System.Collections.Specialized.NameObjectCollectionBase)
See base class member description: System.Collections.Specialized.NameObjectCollectionBase.BaseAdd


Adds an entry with the specified key and value into the NameObjectCollectionBase instance.
BaseClear
(inherited from System.Collections.Specialized.NameObjectCollectionBase)
See base class member description: System.Collections.Specialized.NameObjectCollectionBase.BaseClear


Removes all entries from the NameObjectCollectionBase instance.
BaseGet
(inherited from System.Collections.Specialized.NameObjectCollectionBase)
Overloaded:
BaseGet(int index)

See base class member description: System.Collections.Specialized.NameObjectCollectionBase.BaseGet


Gets the value of the entry at the specified index of the NameObjectCollectionBase instance.
BaseGet
(inherited from System.Collections.Specialized.NameObjectCollectionBase)
Overloaded:
BaseGet(string name)

See base class member description: System.Collections.Specialized.NameObjectCollectionBase.BaseGet


Gets the value of the first entry with the specified key from the NameObjectCollectionBase instance.
BaseGetAllKeys
(inherited from System.Collections.Specialized.NameObjectCollectionBase)
See base class member description: System.Collections.Specialized.NameObjectCollectionBase.BaseGetAllKeys


Returns a String array that contains all the keys in the NameObjectCollectionBase instance.
BaseGetAllValues
(inherited from System.Collections.Specialized.NameObjectCollectionBase)
Overloaded:
BaseGetAllValues()

See base class member description: System.Collections.Specialized.NameObjectCollectionBase.BaseGetAllValues


Returns an Object array that contains all the values in the NameObjectCollectionBase instance.
BaseGetAllValues
(inherited from System.Collections.Specialized.NameObjectCollectionBase)
Overloaded:
BaseGetAllValues(Type type)

See base class member description: System.Collections.Specialized.NameObjectCollectionBase.BaseGetAllValues


Returns an array of the specified type that contains all the values in the NameObjectCollectionBase instance.
BaseGetKey
(inherited from System.Collections.Specialized.NameObjectCollectionBase)
See base class member description: System.Collections.Specialized.NameObjectCollectionBase.BaseGetKey


Gets the key of the entry at the specified index of the NameObjectCollectionBase instance.
BaseHasKeys
(inherited from System.Collections.Specialized.NameObjectCollectionBase)
See base class member description: System.Collections.Specialized.NameObjectCollectionBase.BaseHasKeys


Gets a value indicating whether the NameObjectCollectionBase instance contains entries whose keys are not null.
BaseRemove
(inherited from System.Collections.Specialized.NameObjectCollectionBase)
See base class member description: System.Collections.Specialized.NameObjectCollectionBase.BaseRemove


Removes the entries with the specified key from the NameObjectCollectionBase instance.
BaseRemoveAt
(inherited from System.Collections.Specialized.NameObjectCollectionBase)
See base class member description: System.Collections.Specialized.NameObjectCollectionBase.BaseRemoveAt


Removes the entry at the specified index of the NameObjectCollectionBase instance.
BaseSet
(inherited from System.Collections.Specialized.NameObjectCollectionBase)
Overloaded:
BaseSet(int index, object value)

See base class member description: System.Collections.Specialized.NameObjectCollectionBase.BaseSet


Sets the value of the entry at the specified index of the NameObjectCollectionBase instance.
BaseSet
(inherited from System.Collections.Specialized.NameObjectCollectionBase)
Overloaded:
BaseSet(string name, object value)

See base class member description: System.Collections.Specialized.NameObjectCollectionBase.BaseSet


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.
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
InvalidateCachedArrays Resets the cached arrays of the collection to null.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.Collections.Specialized.NameValueCollection Member Details

Overloaded ctor #1
Summary
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.

Default constructor. This constructor is called by derived class constructors to initialize state in this type.
C# Syntax:
public NameValueCollection();
Remarks
The capacity is the number of key-and-value pairs that the NameValueCollection can contain. The default initial capacity is zero. The capacity is automatically increased as required.

The hash code provider dispenses hash codes for keys in the NameValueCollection. The default hash code provider is the CaseInsensitiveHashCodeProvider.

The comparer determines whether two keys are equal. The default comparer is the CaseInsensitiveComparer.

See also:
CaseInsensitiveHashCodeProvider | CaseInsensitiveComparer

Return to top


Overloaded ctor #2
Summary
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.
C# Syntax:
public NameValueCollection(
   int capacity
);
Parameters:

capacity

The initial number of entries that the NameValueCollection can contain.

Exceptions
Exception Type Condition
ArgumentOutOfRangeException capacity is less than zero.
Remarks
The capacity is the number of key-and-value pairs that the NameValueCollection can contain. The default initial capacity is zero. The capacity is automatically increased as required.

The hash code provider dispenses hash codes for keys in the NameValueCollection. The default hash code provider is the CaseInsensitiveHashCodeProvider.

The comparer determines whether two keys are equal. The default comparer is the CaseInsensitiveComparer.

See also:
CaseInsensitiveHashCodeProvider | CaseInsensitiveComparer

Return to top


Overloaded ctor #3
Summary
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.
C# Syntax:
public NameValueCollection(NameValueCollection(
   NameValueCollection col
);
Parameters:

col

The NameValueCollection to copy to the new NameValueCollection instance.

Exceptions
Exception Type Condition
ArgumentNullException col is null.
Remarks
The capacity is the number of key-and-value pairs that the NameValueCollection can contain. The default initial capacity is zero. The capacity is automatically increased as required.

The hash code provider dispenses hash codes for keys in the NameValueCollection. The default hash code provider is the CaseInsensitiveHashCodeProvider.

The comparer determines whether two keys are equal. The default comparer is the CaseInsensitiveComparer.

The elements of the new NameValueCollection are sorted in the same order as the source NameValueCollection.

See also:
CaseInsensitiveHashCodeProvider | CaseInsensitiveComparer

Return to top


Overloaded ctor #4
Summary
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.
C# Syntax:
public NameValueCollection(
   IHashCodeProvider hashProvider,
   IComparer comparer
);
Parameters:

hashProvider

The IHashCodeProvider that will supply the hash codes for all keys in the NameValueCollection.

comparer

The IComparer to use to determine whether two keys are equal. The IComparer to use to determine whether two keys are equal.

Remarks
The capacity is the number of key-and-value pairs that the NameValueCollection can contain. The default initial capacity is zero. The capacity is automatically increased as required.

The hash code provider dispenses hash codes for keys in the NameValueCollection. The default hash code provider is the CaseInsensitiveHashCodeProvider.

The comparer determines whether two keys are equal. The default comparer is the CaseInsensitiveComparer.

See also:
IHashCodeProvider | CaseInsensitiveHashCodeProvider | IComparer | CaseInsensitiveComparer

Return to top


Overloaded ctor #5
Summary
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.
C# Syntax:
public NameValueCollection(int capacity, NameValueCollection(
   int capacity,
   NameValueCollection col
);
Parameters:

capacity

The initial number of entries that the NameValueCollection can contain.

col

The NameValueCollection to copy to the new NameValueCollection instance.

Exceptions
Exception Type Condition
ArgumentNullException col is null.
Remarks
The capacity is the number of key-and-value pairs that the NameValueCollection can contain. The default initial capacity is zero. The capacity is automatically increased as required.

The hash code provider dispenses hash codes for keys in the NameValueCollection. The default hash code provider is the CaseInsensitiveHashCodeProvider.

The comparer determines whether two keys are equal. The default comparer is the CaseInsensitiveComparer.

See also:
CaseInsensitiveHashCodeProvider | CaseInsensitiveComparer

Return to top


Overloaded ctor #6
Summary
Initializes a new instance of the NameValueCollection class that is serializable and uses the specified SerializationInfo and StreamingContext.
C# Syntax:
protected NameValueCollection(
   SerializationInfo info,
   StreamingContext context
);
Parameters:

info

A SerializationInfo object that contains the information required to serialize the new NameValueCollection instance.

context

A StreamingContext object that contains the source and destination of the serialized stream associated with the new NameValueCollection instance.

See also:
ISerializable | SerializationInfo | StreamingContext

Return to top


Overloaded ctor #7
Summary
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.
C# Syntax:
public NameValueCollection(
   int capacity,
   IHashCodeProvider hashProvider,
   IComparer comparer
);
Parameters:

capacity

The initial number of entries that the NameValueCollection can contain.

hashProvider

The IHashCodeProvider that will supply the hash codes for all keys in the NameValueCollection.

comparer

The IComparer to use to determine whether two keys are equal. The IComparer to use to determine whether two keys are equal.

Exceptions
Exception Type Condition
ArgumentOutOfRangeException capacity is less than zero.
Remarks
The capacity is the number of key-and-value pairs that the NameValueCollection can contain. The default initial capacity is zero. The capacity is automatically increased as required.

The hash code provider dispenses hash codes for keys in the NameValueCollection. The default hash code provider is the CaseInsensitiveHashCodeProvider.

The comparer determines whether two keys are equal. The default comparer is the CaseInsensitiveComparer.

See also:
CaseInsensitiveHashCodeProvider | CaseInsensitiveComparer

Return to top


Property: AllKeys (read-only)
Summary
Gets all the keys in the NameValueCollection.
C# Syntax:
public virtual string[] AllKeys {get;}
Remarks
If the collection is empty, this method returns an empty String array, not null.

The arrays returned by NameValueCollection.AllKeys are cached for better performance and are automatically refreshed when the collection changes. A derived class can invalidate the cached version by calling NameValueCollection.InvalidateCachedArrays, thereby forcing the arrays to be recreated.

See also:
NameValueCollection.GetKey | NameValueCollection.InvalidateCachedArrays

Return to top


Property: Count (read-only)
Inherited
See base class member description: System.Collections.Specialized.NameObjectCollectionBase.Count

Summary
Gets the number of key-and-value pairs contained in the NameObjectCollectionBase instance.
C# Syntax:
public virtual int Count {get;}
Implements:
ICollection.Count

Return to top


Property: IsReadOnly (read-write)
Inherited
See base class member description: System.Collections.Specialized.NameObjectCollectionBase.IsReadOnly

Summary
Gets or sets a value indicating whether the NameObjectCollectionBase instance is read-only.
C# Syntax:
protected bool IsReadOnly {get; set;}
Remarks
A collection that is read-only does not allow the addition, removal, or modification of elements after the collection is created.

Return to top


Overloaded Property: Item (read-only)
Summary
Gets the entry at the specified index of the NameValueCollection.
C# Syntax:
public string this[int index] {get;}
Parameters:

index

The zero-based index of the entry to locate in the collection. The zero-based index of the entry to locate in the collection.

Exceptions
Exception Type Condition
ArgumentOutOfRangeException index is outside the valid range of indexes for the collection.
Remarks
This property provides the ability to access a specific element in the collection by using the following syntax: myCollection[index] .

This property cannot be set. To set the value at a specified index, use Item[GetKey(index)] .

Return to top


Overloaded Property: Item (read-write)
Summary
Gets or sets the entry with the specified key in the NameValueCollection.
C# Syntax:
public string this[string name] {get; set;}
Parameters:

name

The String key of the entry to locate. The key can be null. The String key of the entry to locate. The key can be null.

Exceptions
Exception Type Condition
ArgumentOutOfRangeException index is outside the valid range of indexes for the collection.
NotSupportedException The collection is read-only and the operation attempts to modify the collection.
Remarks
This property provides the ability to access a specific element in the collection by using the following syntax: myCollection[name] .

If the specified key already exists in the collection, setting this property overwrites the existing values with the specified value. To add the new value to the existing list of values, use the NameValueCollection.Add method.

If the specified key does not exist in the collection, setting this property creates a new entry using the specified key and the specified value.

Return to top


Property: Keys (read-only)
Inherited
See base class member description: System.Collections.Specialized.NameObjectCollectionBase.Keys

Summary
Gets a NameObjectCollectionBase.KeysCollection instance that contains all the keys in the NameObjectCollectionBase instance.
C# Syntax:
public virtual NameObjectCollectionBase.KeysCollection Keys {get;}

Return to top


Overloaded Method: Add(
   NameValueCollection c
)
Summary
Copies the entries in the specified NameValueCollection to the current NameValueCollection.
C# Syntax:
public void Add(
   NameValueCollection c
);
Parameters:

c

The NameValueCollection to copy to the current NameValueCollection.

Exceptions
Exception Type Condition
NotSupportedException The collection is read-only.
ArgumentNullException col is null.
Remarks
If a key in c already exists in the target NameValueCollection instance, the associated value in c is added to the existing comma-separated list of values associated with the same key in the target NameValueCollection instance.
See also:
NameValueCollection.Set

Return to top


Overloaded Method: Add(
   string name,
   string value
)
Summary
Adds an entry with the specified name and value to the NameValueCollection.
C# Syntax:
public virtual void Add(
   string name,
   string value
);
Parameters:

name

The String key of the entry to add. The key can be null. The String key of the entry to add. The key can be null.

value

The String value of the entry to add. The value can be null. The String value of the entry to add. The value can be null.

Exceptions
Exception Type Condition
NotSupportedException The collection is read-only.
Remarks
If the specified key already exists in the target NameValueCollection instance, the specified value is added to the existing comma-separated list of values associated with the same key in the target NameValueCollection instance.

If the same value already exists under the same key in the collection, the new value overwrites the old value.

Return to top


Method: BaseAdd(
   string name,
   object value
)
Inherited
See base class member description: System.Collections.Specialized.NameObjectCollectionBase.BaseAdd

Summary
Adds an entry with the specified key and value into the NameObjectCollectionBase instance.
C# Syntax:
protected void BaseAdd(
   string name,
   object value
);
Parameters:

name

The String key of the entry to add. The key can be null. The String key of the entry to add. The key can be null.

value

The Object value of the entry to add. The value can be null. The Object value of the entry to add. The value can be null.

Exceptions
Exception Type Condition
NotSupportedException The collection is read-only.

Return to top


Method: BaseClear()
Inherited
See base class member description: System.Collections.Specialized.NameObjectCollectionBase.BaseClear

Summary
Removes all entries from the NameObjectCollectionBase instance.
C# Syntax:
protected void BaseClear();
Exceptions
Exception Type Condition
NotSupportedException The collection is read-only.
Remarks
NameObjectCollectionBase.Count is set to zero.

Return to top


Overloaded Method: BaseGet(
   int index
)
Inherited
See base class member description: System.Collections.Specialized.NameObjectCollectionBase.BaseGet

Summary
Gets the value of the entry at the specified index of the NameObjectCollectionBase instance.
C# Syntax:
protected object BaseGet(
   int index
);
Parameters:

index

The zero-based index of the value to get.

Return Value:
An Object that represents the value of the entry at the specified index.
Exceptions
Exception Type Condition
ArgumentOutOfRangeException index is outside the valid range of indexes for the collection.

Return to top


Overloaded Method: BaseGet(
   string name
)
Inherited
See base class member description: System.Collections.Specialized.NameObjectCollectionBase.BaseGet

Summary
Gets the value of the first entry with the specified key from the NameObjectCollectionBase instance.
C# Syntax:
protected object BaseGet(
   string name
);
Parameters:

name

The String key of the entry to get. The key can be null. The String key of the entry to get. The key can be null.

Return Value:
An Object that represents the value of the first entry with the specified key, if found; otherwise, null.
Remarks
If the collection contains multiple entries with the specified key, this method returns only the first entry. To get the values of subsequent entries with the same key, use the enumerator to iterate through the collection and compare the keys. This method returns null in the following cases: 1) if the specified key is not found; and 2) if the specified key is found and its associated value is null. This method does not distinguish between the two cases.
See also:
NameObjectCollectionBase.GetEnumerator

Return to top


Method: BaseGetAllKeys()
Inherited
See base class member description: System.Collections.Specialized.NameObjectCollectionBase.BaseGetAllKeys

Summary
Returns a String array that contains all the keys in the NameObjectCollectionBase instance.
C# Syntax:
protected string[] BaseGetAllKeys();
Return Value:
A String array that contains all the keys in the NameObjectCollectionBase instance.

Return to top


Overloaded Method: BaseGetAllValues()
Inherited
See base class member description: System.Collections.Specialized.NameObjectCollectionBase.BaseGetAllValues

Summary
Returns an Object array that contains all the values in the NameObjectCollectionBase instance.
C# Syntax:
protected object[] BaseGetAllValues();
Return Value:
An Object array that contains all the values in the NameObjectCollectionBase instance.

Return to top


Overloaded Method: BaseGetAllValues(
   Type type
)
Inherited
See base class member description: System.Collections.Specialized.NameObjectCollectionBase.BaseGetAllValues

Summary
Returns an array of the specified type that contains all the values in the NameObjectCollectionBase instance.
C# Syntax:
protected object[] BaseGetAllValues(
   Type type
);
Parameters:

type

A Type that represents the type of array to return.

Return Value:
An array of the specified type that contains all the values in the NameObjectCollectionBase instance.
Exceptions
Exception Type Condition
ArgumentNullException type is null.
ArgumentException type is not a valid Type.
See also:
Type

Return to top


Method: BaseGetKey(
   int index
)
Inherited
See base class member description: System.Collections.Specialized.NameObjectCollectionBase.BaseGetKey

Summary
Gets the key of the entry at the specified index of the NameObjectCollectionBase instance.
C# Syntax:
protected string BaseGetKey(
   int index
);
Parameters:

index

The zero-based index of the key to get.

Return Value:
A String that represents the key of the entry at the specified index.
Exceptions
Exception Type Condition
ArgumentOutOfRangeException index is outside the valid range of indexes for the collection.

Return to top


Method: BaseHasKeys()
Inherited
See base class member description: System.Collections.Specialized.NameObjectCollectionBase.BaseHasKeys

Summary
Gets a value indicating whether the NameObjectCollectionBase instance contains entries whose keys are not null.
C# Syntax:
protected bool BaseHasKeys();
Return Value:
true if the NameObjectCollectionBase instance contains entries whose keys are not null; otherwise, false.

Return to top


Method: BaseRemove(
   string name
)
Inherited
See base class member description: System.Collections.Specialized.NameObjectCollectionBase.BaseRemove

Summary
Removes the entries with the specified key from the NameObjectCollectionBase instance.
C# Syntax:
protected void BaseRemove(
   string name
);
Parameters:

name

The String key of the entries to remove. The key can be null. The String key of the entries to remove. The key can be null.

Exceptions
Exception Type Condition
NotSupportedException The collection is read-only.

-or-

The collection has a fixed size.

Remarks
In collections of contiguous elements, such as lists, the elements that follow the removed element move up to occupy the vacated spot. If the collection is indexed, the indexes of the elements that are moved are also updated. This behavior does not apply to collections where elements are conceptually grouped into buckets, such as a hashtable.

Return to top


Method: BaseRemoveAt(
   int index
)
Inherited
See base class member description: System.Collections.Specialized.NameObjectCollectionBase.BaseRemoveAt

Summary
Removes the entry at the specified index of the NameObjectCollectionBase instance.
C# Syntax:
protected void BaseRemoveAt(
   int index
);
Parameters:

index

The zero-based index of the entry to remove.

Exceptions
Exception Type Condition
ArgumentOutOfRangeException index is outside the valid range of indexes for the collection.
NotSupportedException The collection is read-only.

-or-

The collection has a fixed size.

Remarks
In collections of contiguous elements, such as lists, the elements that follow the removed element move up to occupy the vacated spot. If the collection is indexed, the indexes of the elements that are moved are also updated. This behavior does not apply to collections where elements are conceptually grouped into buckets, such as a hashtable.

Return to top


Overloaded Method: BaseSet(
   int index,
   object value
)
Inherited
See base class member description: System.Collections.Specialized.NameObjectCollectionBase.BaseSet

Summary
Sets the value of the entry at the specified index of the NameObjectCollectionBase instance.
C# Syntax:
protected void BaseSet(
   int index,
   object value
);
Parameters:

index

The zero-based index of the entry to set.

value

The Object that represents the new value of the entry to set. The value can be null. The Object that represents the new value of the entry to set. The value can be null.

Exceptions
Exception Type Condition
NotSupportedException The collection is read-only.
ArgumentOutOfRangeException index is outside the valid range of indexes for the collection.

Return to top


Overloaded Method: BaseSet(
   string name,
   object value
)
Inherited
See base class member description: System.Collections.Specialized.NameObjectCollectionBase.BaseSet

Summary
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.
C# Syntax:
protected void BaseSet(
   string name,
   object value
);
Parameters:

name

The String key of the entry to set. The key can be null. The String key of the entry to set. The key can be null.

value

The Object that represents the new value of the entry to set. The value can be null. The Object that represents the new value of the entry to set. The value can be null.

Exceptions
Exception Type Condition
NotSupportedException The collection is read-only.
Remarks
If the collection contains multiple entries with the specified key, this method sets only the first entry. To set the values of subsequent entries with the same key, use the enumerator to iterate through the collection and compare the keys.
See also:
NameObjectCollectionBase.GetEnumerator

Return to top


Method: Clear()
Summary
Invalidates the cached arrays and removes all entries from the NameValueCollection.
C# Syntax:
public void Clear();
Exceptions
Exception Type Condition
NotSupportedException The collection is read-only.
See also:
NameValueCollection.InvalidateCachedArrays

Return to top


Method: CopyTo(
   Array dest,
   int index
)
Summary
Copies the entire NameValueCollection to a compatible one-dimensional Array, starting at the specified index of the target array.
C# Syntax:
public void CopyTo(
   Array dest,
   int index
);
Parameters:

dest

The one-dimensional Array that is the destination of the elements copied from NameValueCollection. The Array must have zero-based indexing.

index

The zero-based index in dest at which copying begins.

Exceptions
Exception Type Condition
ArgumentNullException dest is null.
ArgumentOutOfRangeException index is less than zero.
ArgumentException dest is multidimensional.

-or-

index is equal to or greater than the length of dest.

-or-

The number of elements in the source NameValueCollection is greater than the available space from index to the end of the destination dest.

InvalidCastException The type of the source NameValueCollection cannot be cast automatically to the type of the destination dest.
Remarks
The specified array must be of a compatible type.

This method uses Array.Copy to copy the elements.

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~NameValueCollection();

For more information on members inherited from System.Object click on the link above.

Return to top


Overloaded Method: Get(
   int index
)
Summary
Gets the values at the specified index of the NameValueCollection combined into one comma-separated list.
C# Syntax:
public virtual string Get(
   int index
);
Parameters:

index

The zero-based index of the entry that contains the values to get from the collection. The zero-based index of the entry that contains the values to get from the collection.

Return Value:
A String that contains a comma-separated list of the values at the specified index of the NameValueCollection, if found; otherwise, null.
Exceptions
Exception Type Condition
ArgumentOutOfRangeException index is outside the valid range of indexes for the collection.
See also:
NameValueCollection.GetValues | NameValueCollection.AllKeys

Return to top


Overloaded Method: Get(
   string name
)
Summary
Gets the values associated with the specified key from the NameValueCollection combined into one comma-separated list.
C# Syntax:
public virtual string Get(
   string name
);
Parameters:

name

The String key of the entry that contains the values to get. The key can be null. The String key of the entry that contains the values to get. The key can be null.

Return Value:
A String that contains a comma-separated list of the values associated with the specified key from the NameValueCollection, if found; otherwise, null.
See also:
NameValueCollection.GetValues | NameValueCollection.AllKeys

Return to top


Method: GetEnumerator()
Inherited
See base class member description: System.Collections.Specialized.NameObjectCollectionBase.GetEnumerator

Summary
Returns an enumerator that can iterate through the NameObjectCollectionBase.
C# Syntax:
public IEnumerator GetEnumerator();
Return Value:
An IEnumerator for the NameObjectCollectionBase instance.
Implements:
IEnumerable.GetEnumerator
Remarks
This enumerator returns the keys of the collection as strings.

Enumerators only allow reading the data in the collection. Enumerators cannot be used to modify the underlying collection.

Initially, the enumerator is positioned before the first element in the collection. IEnumerator.Reset also brings the enumerator back to this position. At this position, calling IEnumerator.Current throws an exception. Therefore, you must call IEnumerator.MoveNext to advance the enumerator to the first element of the collection before reading the value of IEnumerator.Current.

IEnumerator.Current returns the same object until either IEnumerator.MoveNext or IEnumerator.Reset is called. IEnumerator.MoveNext sets IEnumerator.Current to the next element.

After the end of the collection is passed, the enumerator is positioned after the last element in the collection, and calling IEnumerator.MoveNext returns false. If the last call to IEnumerator.MoveNext returned false, calling IEnumerator.Current throws an exception. To set IEnumerator.Current to the first element of the collection again, you can call IEnumerator.Reset followed by IEnumerator.MoveNext.

An enumerator remains valid as long as the collection remains unchanged. If changes are made to the collection, such as adding, modifying or deleting elements, the enumerator is irrecoverably invalidated and the next call to IEnumerator.MoveNext or IEnumerator.Reset throws an InvalidOperationException. If the collection is modified between IEnumerator.MoveNext and IEnumerator.Current, IEnumerator.Current will return the element that it is set to, even if the enumerator is already invalidated.

The enumerator does not have exclusive access to the collection; therefore, enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could 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.

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetKey(
   int index
)
Summary
Gets the key at the specified index of the NameValueCollection.
C# Syntax:
public virtual string GetKey(
   int index
);
Parameters:

index

The zero-based index of the key to get from the collection. The zero-based index of the key to get from the collection.

Return Value:
A String that contains the key at the specified index of the NameValueCollection, if found; otherwise, null.
Exceptions
Exception Type Condition
ArgumentOutOfRangeException index is outside the valid range of indexes for the collection.
See also:
NameValueCollection.AllKeys

Return to top


Method: GetObjectData(
   SerializationInfo info,
   StreamingContext context
)
Inherited
See base class member description: System.Collections.Specialized.NameObjectCollectionBase.GetObjectData

Summary
Implements the ISerializable interface and returns the data needed to serialize the NameObjectCollectionBase instance.
C# Syntax:
public virtual void GetObjectData(
   SerializationInfo info,
   StreamingContext context
);
Parameters:

info

A SerializationInfo object that contains the information required to serialize the NameObjectCollectionBase instance.

context

A StreamingContext object that contains the source and destination of the serialized stream associated with the NameObjectCollectionBase instance.

Exceptions
Exception Type Condition
ArgumentNullException info is null.
Implements:
ISerializable.GetObjectData
See also:
ISerializable | SerializationInfo | StreamingContext

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Overloaded Method: GetValues(
   int index
)
Summary
Gets the values at the specified index of the NameValueCollection.
C# Syntax:
public virtual string[] GetValues(
   int index
);
Parameters:

index

The zero-based index of the entry that contains the values to get from the collection. The zero-based index of the entry that contains the values to get from the collection.

Return Value:
A String array that contains the values at the specified index of the NameValueCollection, if found; otherwise, null.
Exceptions
Exception Type Condition
ArgumentOutOfRangeException index is outside the valid range of indexes for the collection.
See also:
NameValueCollection.Get | NameValueCollection.AllKeys

Return to top


Overloaded Method: GetValues(
   string name
)
Summary
Gets the values associated with the specified key from the NameValueCollection.
C# Syntax:
public virtual string[] GetValues(
   string name
);
Parameters:

name

The String key of the entry that contains the values to get. The key can be null. The String key of the entry that contains the values to get. The key can be null.

Return Value:
A String array that contains the values associated with the specified key from the NameValueCollection, if found; otherwise, null.
See also:
NameValueCollection.Get | NameValueCollection.AllKeys

Return to top


Method: HasKeys()
Summary
Gets a value indicating whether the NameValueCollection contains keys that are not null.
C# Syntax:
public bool HasKeys();
Return Value:
true if the NameValueCollection contains keys that are not null; otherwise, false.

Return to top


Method: InvalidateCachedArrays()
Summary
Resets the cached arrays of the collection to null.
C# Syntax:
protected void InvalidateCachedArrays();
Remarks
The arrays returned by NameValueCollection.AllKeys are cached for better performance and are automatically refreshed when the collection changes. A derived class can invalidate the cached version by calling NameValueCollection.InvalidateCachedArrays, thereby forcing the arrays to be recreated.
See also:
NameValueCollection.AllKeys

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: OnDeserialization(
   object sender
)
Inherited
See base class member description: System.Collections.Specialized.NameObjectCollectionBase.OnDeserialization

Summary
Implements the ISerializable interface and raises the deserialization event when the deserialization is complete.
C# Syntax:
public virtual void OnDeserialization(
   object sender
);
Parameters:

sender

The source of the deserialization event.

Exceptions
Exception Type Condition
SerializationException The SerializationInfo object associated with the current NameObjectCollectionBase instance is invalid.
Implements:
IDeserializationCallback.OnDeserialization
See also:
ISerializable | NameObjectCollectionBase.GetObjectData

Return to top


Method: Remove(
   string name
)
Summary
Removes the entries with the specified key from the NameObjectCollectionBase instance.
C# Syntax:
public virtual void Remove(
   string name
);
Parameters:

name

The String key of the entry to remove. The key can be null.

Exceptions
Exception Type Condition
NotSupportedException The collection is read-only.

-or-

The collection has a fixed size.

Remarks
In collections of contiguous elements, such as lists, fthe elements that follow the removed element move up to occupy the vacated spot. If the collection is indexed, the indexes of the elements that are moved are also updated. This behavior does not apply to collections where elements are conceptually grouped into buckets, such as a hashtable.

Return to top


Method: Set(
   string name,
   string value
)
Summary
Sets the value of an entry in the NameValueCollection.
C# Syntax:
public virtual void Set(
   string name,
   string value
);
Parameters:

name

The String key of the entry to add the new value to. The key can be null. The String key of the entry to add the new value to. The key can be null.

value

The Object that represents the new value to add to the specified entry. The value can be null. The Object that represents the new value to add to the specified entry. The value can be null.

Exceptions
Exception Type Condition
NotSupportedException The collection is read-only.
Remarks
If the specified key already exists in the collection, this method overwrites the existing values with the specified value. To add the new value to the existing list of values, use the NameValueCollection.Add method.

If the specified key does not exist in the collection, this method creates a new entry using the specified key and the specified value.

See also:
NameValueCollection.Add

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.