Export (0) Print
Expand All

StringCollection.Add Method

Adds a string to the end of the StringCollection.

[Visual Basic]
Public Function Add( _
   ByVal value As String _
) As Integer
[C#]
public int Add(
 string value
);
[C++]
public: int Add(
 String* value
);
[JScript]
public function Add(
   value : String
) : int;

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.

Remarks

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

Example

[Visual Basic, C#, C++] The following code example adds new elements to the StringCollection.

[Visual Basic] 
Imports System
Imports System.Collections
Imports System.Collections.Specialized

Public Class SamplesStringCollection   

   Public Shared Sub Main()

      ' Creates and initializes a new StringCollection.
      Dim myCol As 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.
      Dim myArr() As [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)

   End Sub 'Main

   Public Shared Sub PrintValues(myCol As IEnumerable)
      Dim myEnumerator As System.Collections.IEnumerator = myCol.GetEnumerator()

      While myEnumerator.MoveNext()
         Console.WriteLine("   {0}", myEnumerator.Current)
      End While
      Console.WriteLine()

   End Sub 'PrintValues

End Class '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
'


[C#] 
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 )  {
      System.Collections.IEnumerator myEnumerator = myCol.GetEnumerator();
      while ( myEnumerator.MoveNext() )
         Console.WriteLine( "   {0}", myEnumerator.Current );
      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

*/

[C++] 
#using <mscorlib.dll>
#using <System.dll>

using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;

void PrintValues(IEnumerable* myCol) {
   System::Collections::IEnumerator* myEnumerator = myCol->GetEnumerator();
   while (myEnumerator->MoveNext())
      Console::WriteLine(S"   {0}", myEnumerator->Current);
   Console::WriteLine();
}

int main() {

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

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

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

   Console::WriteLine(S"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(S"* white");
   myCol->Insert(3, S"* gray");

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

/*
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

*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

StringCollection Class | StringCollection Members | System.Collections.Specialized Namespace | AddRange | IsReadOnly

Show:
© 2014 Microsoft