Represents a collection of associated String keys and String values : NameObjectCollectionBase « Collections Data Structure « C# / C Sharp






Represents a collection of associated String keys and String values

 

using System;
using System.Collections;
using System.Collections.Specialized;

public class SamplesNameValueCollection  {

   public static void Main()  {
      NameValueCollection myCol = new NameValueCollection();
      myCol.Add( "A", "a" );
      myCol.Add( "B", "b" );
      myCol.Add( "C", "c" );
      PrintKeysAndValues( myCol );
      PrintKeysAndValues2( myCol );
      Console.WriteLine( "Index 1 contains the value {0}.", myCol[1] );
      Console.WriteLine( "Key \"A\" has the value {0}.", myCol["A"] );
      String[] myStrArr = new String[myCol.Count];
      myCol.CopyTo( myStrArr, 0 );
      foreach ( String s in myStrArr )
         Console.WriteLine( "   {0}", s );
      myCol.Remove( "A" );
      PrintKeysAndValues( myCol );
      myCol.Clear();
      PrintKeysAndValues( myCol );
   }

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

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

   
  








Related examples in the same category

1.NameObjectCollectionBase Demo
2.NameObjectCollectionBase is the base class for a collection of associated String keys and Object values
3.Adds an entry with the specified key and value into NameObjectCollectionBase
4.Removes all entries from NameObjectCollectionBase
5.Gets the value of the entry at the specified index of NameObjectCollectionBase
6.Returns a String array that contains all the keys in NameObjectCollectionBase
7.Whether NameObjectCollectionBase contains entries whose keys are not null.
8.Removes the entries with the specified key from NameObjectCollectionBase
9.Sets the value of the entry at the specified index of NameObjectCollectionBase
10.Whether NameObjectCollectionBase is read-only.