NameValueCollection Class

Summary

Represents a collection of key/value pairs that are accessible by key or by index. The keys are strings, and the values are also strings. The keys are not case-sensitive.

Namespace: System.Collections.Specialized

Assembly: System (in System.dll)

Members

  • Constructor NameValueCollection() public NameValueCollection()

    Initializes a new instance of the NameValueCollection class that is empty, has the default initial capacity, and uses case-insensitive comparer.

  • Constructor NameValueCollection(NameValueCollection template) public NameValueCollection(NameValueCollection template)

    Initializes a new instance of the NameValueCollection class that contains elements copied from the specified collection and that has sufficient capacity to accommodate the number of elements copied.

  • Property AllKeys public string[] AllKeys { get; }

    Gets a string array that contains all the keys in the collection. The keys are not sorted and may appear multiple times.

  • Property Comparer public System.Collections.IComparer Comparer { get; }

    Gets the comparison logic for the keys in the collection.

  • Method Add(string name, string value) public virtual void Add(string name, string value)

    Adds an entry with the specified name and value to the NameValueCollection.

  • Method Add(NameValueCollection c) public virtual void Add(NameValueCollection c)

    Adds the entries in the specified NameValueCollection to this NameValueCollection.

  • Method Clear() public virtual void Clear()

    Removes all entries from the NameValueCollection.

  • Method Get(int index) public virtual string Get(int index)

    Gets the value, including the hierarchy, associated with the specified index.

  • Method GetKey(int index) public virtual string GetKey(int index)

    Gets the decoded key associated with the specified index in the NameValueCollection.

  • Method Remove(string name) public virtual void Remove(string name)

    Removes the entries with the specified name from the NameValueCollection.

  • Method Set(string name, string value) public virtual void Set(string name, string value)

    Replaces the entry with the specified name with a new entry with the specified name and value.

Remarks

The NameValueCollection class is useful for storing related names and values. For example, you can use it to store pairs of HTTP header names and values or query string parameters.

Keys in a NameValueCollection are not required to be unique. If multiple entries have the same key, the GetValues(string name) method can be used to retrieve all values associated with that key.

The NameValueCollection is case-insensitive by default. This behavior can be changed by passing a System.Collections.IComparer object to the constructor.

Example

The following example demonstrates how to create and use a NameValueCollection to store and retrieve key-value pairs.

using System;
using System.Collections.Specialized;

public class Example
{
    public static void Main()
    {
        // Create a new NameValueCollection
        NameValueCollection nvCollection = new NameValueCollection();

        // Add key-value pairs
        nvCollection.Add("name", "Alice");
        nvCollection.Add("age", "30");
        nvCollection.Add("city", "New York");
        nvCollection.Add("name", "Bob"); // Adding a duplicate key

        // Access values by key
        Console.WriteLine($"Name: {nvCollection["name"]}"); // Gets the first value for "name"
        Console.WriteLine($"Age: {nvCollection["age"]}");
        Console.WriteLine($"City: {nvCollection["city"]}");

        // Retrieve all values for a key
        string[] names = nvCollection.GetValues("name");
        Console.WriteLine("All names:");
        foreach (string n in names)
        {
            Console.WriteLine($"- {n}");
        }

        // Iterate through all keys and values
        Console.WriteLine("\nAll entries:");
        foreach (string key in nvCollection)
        {
            Console.WriteLine($"Key: {key}, Value: {nvCollection[key]}");
        }

        // Remove an entry
        nvCollection.Remove("age");
        Console.WriteLine($"\nAge after removal: {nvCollection["age"]}"); // Output: (null)

        // Get all keys
        Console.WriteLine("\nAll keys in the collection:");
        foreach (string k in nvCollection.AllKeys)
        {
            Console.WriteLine(k);
        }
    }
}

Requirements

Namespace: System.Collections.Specialized

Assembly: System (in System.dll)

.NET Framework: Supported in: 4.8, 4.7.2, 4.7.1, 4.7, 4.6.2, 4.6.1, 4.6, 4.5.2, 4.5.1, 4.5, 4.0, 3.5, 3.0, 2.0

.NET: Supported in: 7.0, 6.0, 5.0, .NET Core 3.1, .NET Core 2.1, .NET Standard 2.1, .NET Standard 2.0