Export (0) Print
Expand All

StringCollection.Add Method

Adds a string to the end of the StringCollection.

Namespace: System.Collections.Specialized
Assembly: System (in system.dll)

public int Add (
	string value
)
public int Add (
	String value
)
public function Add (
	value : String
) : int
Not applicable.

Parameters

value

The string to add to the end of the StringCollection. The value can be a null reference (Nothing in Visual Basic).

Return Value

The zero-based index at which the new element is inserted.

StringCollection accepts a null reference (Nothing in Visual Basic) as a valid value and allows duplicate elements.

If Count is less than the capacity, this method is an O(1) operation. If the capacity needs to be increased to accommodate the new element, this method becomes an O(n) operation, where n is Count.

The following code example adds new elements to the StringCollection.

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

public class SamplesStringCollection  {

   public static void Main()  {

      // Creates and initializes a new StringCollection.
      StringCollection myCol = new StringCollection();

      Console.WriteLine( "Initial contents of the StringCollection:" );
      PrintValues( myCol );

      // Adds a range of elements from an array to the end of the StringCollection.
      String[] myArr = new String[] { "RED", "orange", "yellow", "RED", "green", "blue", "RED", "indigo", "violet", "RED" };
      myCol.AddRange( myArr );

      Console.WriteLine( "After adding a range of elements:" );
      PrintValues( myCol );

      // Adds one element to the end of the StringCollection and inserts another at index 3.
      myCol.Add( "* white" );
      myCol.Insert( 3, "* gray" );

      Console.WriteLine( "After adding \"* white\" to the end and inserting \"* gray\" at index 3:" );
      PrintValues( myCol );

   }

   public static void PrintValues( IEnumerable myCol )  {
      foreach ( Object obj in myCol )
         Console.WriteLine( "   {0}", obj );
      Console.WriteLine();
   }

}

/*
This code produces the following output.

Initial contents of the StringCollection:

After adding a range of elements:
   RED
   orange
   yellow
   RED
   green
   blue
   RED
   indigo
   violet
   RED

After adding "* white" to the end and inserting "* gray" at index 3:
   RED
   orange
   yellow
   * gray
   RED
   green
   blue
   RED
   indigo
   violet
   RED
   * white

*/

import System.* ;
import System.Collections.* ;
import System.Collections.Specialized.* ;

public class SamplesStringCollection
{      
    public static void main(String[] args)
    {        
        // Creates and initializes a new StringCollection.
        StringCollection myCol =  new StringCollection();
        
        Console.WriteLine("Initial contents of the StringCollection:");
        PrintValues(myCol);
        
        // Adds a range of elements from an array to the end of the 
        // StringCollection.
        String myArr[] = new String[]{"RED", "orange", "yellow", "RED", 
            "green", "blue", "RED", "indigo", "violet", "RED"};
        myCol.AddRange(myArr);
        
        Console.WriteLine("After adding a range of elements:");
        PrintValues(myCol);
        
        // Adds one element to the end of the StringCollection and inserts
        // another at index 3.
        myCol.Add("* white");
        myCol.Insert(3, "* gray");
        
        Console.WriteLine("After adding \"* white\" to the end and inserting"
            + " \"* gray\" at index 3:");
        PrintValues(myCol);
    } //main

    public static void PrintValues(IEnumerable myCol) 
    {
        Object obj = null;
        IEnumerator objEnum = myCol.GetEnumerator();

        while (objEnum.MoveNext()) { 
            obj = objEnum.get_Current();
            Console.WriteLine("   {0}", obj);
        }

        Console.WriteLine();
    } //PrintValues
} //SamplesStringCollection 

/*
This code produces the following output.

Initial contents of the StringCollection:

After adding a range of elements:
   RED
   orange
   yellow
   RED
   green
   blue
   RED
   indigo
   violet
   RED

After adding "* white" to the end and inserting "* gray" at index 3:
   RED
   orange
   yellow
   * gray
   RED
   green
   blue
   RED
   indigo
   violet
   RED
   * white

*/

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft