허당 레몬도리

NameValueCollection 클래스

참고 :http://blog.lemondory.com/279 (HttpUtility.ParseQueryString)

키나 인덱스를 사용하여 액세스할 수 있는 연결된 String 키와 String 값의 컬렉션을 나타냅니다.


설명


이 컬렉션이 기반는 NameObjectCollectionBase 클래스입니다. 그러나 달리는 NameObjectCollectionBase, 이 클래스는 하나의 키 아래에 있는 여러 문자열 값을 저장 합니다.

헤더, 쿼리 문자열 및 양식 데이터에 대 한이 클래스를 사용할 수 있습니다.

각 요소는 키/값 쌍입니다.

이 형식의 컬렉션 요소의 순서를 유지 하지 않는 및 컬렉션을 열거 하는 동안 특정 순서가 보장 됩니다.

용량을 NameValueCollection 요소 수는는 NameValueCollection 보유할 수 있습니다. 요소에 추가 되는NameValueCollection, 용량을 증가 시켜 자동으로 재할당을 통해 필요에 따라 합니다.

키에 대 한 해시 코드를 분배 하는 해시 코드 공급자는 NameValueCollection합니다. 기본 해시 코드 공급자는는CaseInsensitiveHashCodeProvider합니다.

비교자는 두 키가 같은지 여부를 결정 합니다. 기본 비교자는는 CaseInsensitiveComparer합니다.

.NET Framework 버전 1.0에서는이 클래스는 문화권 구분 문자열 비교를 사용합니다. .NET Framework 버전 1.1 이상에서는이 클래스가 사용 하는 반면 CultureInfo.InvariantCulture 문자열을 비교할 때. Culture 비교 및 정렬에 미치는 영향에 대 한 자세한 내용은 참조 Culture의 영향을 받지 않는 문자열 작업 수행합니다.

null 키 또는 값으로 허용 됩니다.

주의 정보주의

Get 메서드를 구분 하지 않는 null 지정된 된 키를 찾을 수 없어서 반환 되는 및 null 키에 연결 된 값이 반환 되는 null합니다.

!! Example

 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();
   }

 


profile

허당 레몬도리

@LemonDory

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!