Click here to Skip to main content
13,554,833 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 23 Apr 2013
Licenced CPOL

The NameValueCollection quick example

Rate this:
Please Sign up or sign in to vote.
A quick example on how to use the NameValueCollection class


One of the most common questions I've been asked is "How can I make a collection that allows me to enter either a key or an index to pull a value?" (well, usually it's formed more like "I want to build a property in my class that works just like the .Items property in x class", "How can I make a collection that works like AppSettings["key"], or some other specific but related question)

I've been asked this question often enough that I'm just going to put together a quick tip / example so people can reference it whenever they need to.


You'll need to reference the System.Collections.Specialized namespace and use the NameValueCollection class

Using the code 

Below is a quick console application that creates a NameValueCollection variable, Populates it with data, and then displays one of the values to the console. Very simple example code that you can reference and expand upon in your own projects.

using System;
using System.Collections.Specialized;

namespace ConsoleApplication1
  /// <summary>
  /// An incredibly simple demonstration of
  /// NameValueCollection
  /// </summary>
  class Program
    static void Main(string[] args)
      // Say we want a simple string collection. Similar to AppSettings or the like.
      // make a collection
      NameValueCollection tags = new NameValueCollection();
      // quick loop to populate it with some demo values.
      for (int i = 1; i <= 10; i++)
        tags.Add(string.Format("tag{0}",i.ToString()),string.Format("The value of tag{0}",i.ToString()));
      // in this case we know there is an index of tag2 so we'll hard
      // code for the demo



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

George H. Slaterpryce III
United States United States
Developing software since 1995, starting out with Borland Turbo C++ 3 & Motorolla Assembly.
Eventually learning several different languages such as
Pascal/Delphi,Basic, Visual Basic,FoxPro, ASP, PHP, and Perl.

Currently using the .net framework for most development task.

Hobbies include underwater robotics, and electronics.

George's current language of choice is C#.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 4 Pin
Jason Down1-May-13 3:25
memberJason Down1-May-13 3:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180515.1 | Last Updated 23 Apr 2013
Article Copyright 2013 by George H. Slaterpryce III
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid