StringCollection Klass
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Representerar en samling strängar.
public ref class StringCollection : System::Collections::IList
public class StringCollection : System.Collections.IList
[System.Serializable]
public class StringCollection : System.Collections.IList
type StringCollection = class
interface ICollection
interface IEnumerable
interface IList
[<System.Serializable>]
type StringCollection = class
interface IList
interface ICollection
interface IEnumerable
Public Class StringCollection
Implements IList
- Arv
-
StringCollection
- Härledda
- Attribut
- Implementeringar
Exempel
I följande kodexempel visas flera av egenskaperna och metoderna StringCollectionför .
using System;
using System.Collections;
using System.Collections.Specialized;
public class SamplesStringCollection {
public static void Main() {
// Create and initializes a new StringCollection.
StringCollection myCol = new StringCollection();
// Add 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 );
// Display the contents of the collection using foreach. This is the preferred method.
Console.WriteLine( "Displays the elements using foreach:" );
PrintValues1( myCol );
// Display the contents of the collection using the enumerator.
Console.WriteLine( "Displays the elements using the IEnumerator:" );
PrintValues2( myCol );
// Display the contents of the collection using the Count and Item properties.
Console.WriteLine( "Displays the elements using the Count and Item properties:" );
PrintValues3( myCol );
// Add one element to the end of the StringCollection and insert 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:" );
PrintValues1( myCol );
// Remove one element from the StringCollection.
myCol.Remove( "yellow" );
Console.WriteLine( "After removing \"yellow\":" );
PrintValues1( myCol );
// Remove all occurrences of a value from the StringCollection.
int i = myCol.IndexOf( "RED" );
while ( i > -1 ) {
myCol.RemoveAt( i );
i = myCol.IndexOf( "RED" );
}
// Verify that all occurrences of "RED" are gone.
if ( myCol.Contains( "RED" ) )
Console.WriteLine( "*** The collection still contains \"RED\"." );
Console.WriteLine( "After removing all occurrences of \"RED\":" );
PrintValues1( myCol );
// Copy the collection to a new array starting at index 0.
String[] myArr2 = new String[myCol.Count];
myCol.CopyTo( myArr2, 0 );
Console.WriteLine( "The new array contains:" );
for ( i = 0; i < myArr2.Length; i++ ) {
Console.WriteLine( " [{0}] {1}", i, myArr2[i] );
}
Console.WriteLine();
// Clears the entire collection.
myCol.Clear();
Console.WriteLine( "After clearing the collection:" );
PrintValues1( myCol );
}
// Uses the foreach statement which hides the complexity of the enumerator.
// NOTE: The foreach statement is the preferred way of enumerating the contents of a collection.
public static void PrintValues1( StringCollection myCol ) {
foreach ( Object obj in myCol )
Console.WriteLine( " {0}", obj );
Console.WriteLine();
}
// Uses the enumerator.
// NOTE: The foreach statement is the preferred way of enumerating the contents of a collection.
public static void PrintValues2( StringCollection myCol ) {
StringEnumerator myEnumerator = myCol.GetEnumerator();
while ( myEnumerator.MoveNext() )
Console.WriteLine( " {0}", myEnumerator.Current );
Console.WriteLine();
}
// Uses the Count and Item properties.
public static void PrintValues3( StringCollection myCol ) {
for ( int i = 0; i < myCol.Count; i++ )
Console.WriteLine( " {0}", myCol[i] );
Console.WriteLine();
}
}
/*
This code produces the following output.
Displays the elements using foreach:
RED
orange
yellow
RED
green
blue
RED
indigo
violet
RED
Displays the elements using the IEnumerator:
RED
orange
yellow
RED
green
blue
RED
indigo
violet
RED
Displays the elements using the Count and Item properties:
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
After removing "yellow":
RED
orange
* gray
RED
green
blue
RED
indigo
violet
RED
* white
After removing all occurrences of "RED":
orange
* gray
green
blue
indigo
violet
* white
The new array contains:
[0] orange
[1] * gray
[2] green
[3] blue
[4] indigo
[5] violet
[6] * white
After clearing the collection:
*/
Imports System.Collections
Imports System.Collections.Specialized
Public Class SamplesStringCollection
Public Shared Sub Main()
' Create and initializes a new StringCollection.
Dim myCol As New StringCollection()
' Add 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)
' Display the contents of the collection using foreach. This is the preferred method.
Console.WriteLine("Displays the elements using foreach:")
PrintValues1(myCol)
' Display the contents of the collection using the enumerator.
Console.WriteLine("Displays the elements using the IEnumerator:")
PrintValues2(myCol)
' Display the contents of the collection using the Count and Item properties.
Console.WriteLine("Displays the elements using the Count and Item properties:")
PrintValues3(myCol)
' Add one element to the end of the StringCollection and insert 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:")
PrintValues1(myCol)
' Remove one element from the StringCollection.
myCol.Remove("yellow")
Console.WriteLine("After removing ""yellow"":")
PrintValues1(myCol)
' Remove all occurrences of a value from the StringCollection.
Dim i As Integer = myCol.IndexOf("RED")
While i > - 1
myCol.RemoveAt(i)
i = myCol.IndexOf("RED")
End While
' Verify that all occurrences of "RED" are gone.
If myCol.Contains("RED") Then
Console.WriteLine("*** The collection still contains ""RED"".")
End If
Console.WriteLine("After removing all occurrences of ""RED"":")
PrintValues1(myCol)
' Copy the collection to a new array starting at index 0.
Dim myArr2(myCol.Count) As String
myCol.CopyTo(myArr2, 0)
Console.WriteLine("The new array contains:")
For i = 0 To myArr2.Length - 1
Console.WriteLine(" [{0}] {1}", i, myArr2(i))
Next i
Console.WriteLine()
' Clears the entire collection.
myCol.Clear()
Console.WriteLine("After clearing the collection:")
PrintValues1(myCol)
End Sub
' Uses the foreach statement which hides the complexity of the enumerator.
' NOTE: The foreach statement is the preferred way of enumerating the contents of a collection.
Public Shared Sub PrintValues1(myCol As StringCollection)
Dim obj As [Object]
For Each obj In myCol
Console.WriteLine(" {0}", obj)
Next obj
Console.WriteLine()
End Sub
' Uses the enumerator.
' NOTE: The foreach statement is the preferred way of enumerating the contents of a collection.
Public Shared Sub PrintValues2(myCol As StringCollection)
Dim myEnumerator As StringEnumerator = myCol.GetEnumerator()
While myEnumerator.MoveNext()
Console.WriteLine(" {0}", myEnumerator.Current)
End While
Console.WriteLine()
End Sub
' Uses the Count and Item properties.
Public Shared Sub PrintValues3(myCol As StringCollection)
Dim i As Integer
For i = 0 To myCol.Count - 1
Console.WriteLine(" {0}", myCol(i))
Next i
Console.WriteLine()
End Sub
End Class
'This code produces the following output.
'
'Displays the elements using foreach:
' RED
' orange
' yellow
' RED
' green
' blue
' RED
' indigo
' violet
' RED
'
'Displays the elements using the IEnumerator:
' RED
' orange
' yellow
' RED
' green
' blue
' RED
' indigo
' violet
' RED
'
'Displays the elements using the Count and Item properties:
' 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
'
'After removing "yellow":
' RED
' orange
' * gray
' RED
' green
' blue
' RED
' indigo
' violet
' RED
' * white
'
'After removing all occurrences of "RED":
' orange
' * gray
' green
' blue
' indigo
' violet
' * white
'
'The new array contains:
' [0] orange
' [1] * gray
' [2] green
' [3] blue
' [4] indigo
' [5] violet
' [6] * white
'
'After clearing the collection:
'
Kommentarer
StringCollection
null accepterar som ett giltigt värde och tillåter duplicerade element.
Strängjämförelser är skiftlägeskänsliga.
Element i den här samlingen kan nås med hjälp av ett heltalsindex. Index i den här samlingen är nollbaserade.
Konstruktorer
| Name | Description |
|---|---|
| StringCollection() |
Initierar en ny instans av StringCollection klassen. |
Egenskaper
| Name | Description |
|---|---|
| Count |
Hämtar antalet strängar som finns i StringCollection. |
| IsReadOnly |
Hämtar ett värde som anger om är StringCollection skrivskyddat. |
| IsSynchronized |
Hämtar ett värde som anger om åtkomsten StringCollection till är synkroniserad (trådsäker). |
| Item[Int32] |
Hämtar eller anger elementet vid det angivna indexet. |
| SyncRoot |
Hämtar ett objekt som kan användas för att synkronisera åtkomsten StringCollectiontill . |
Metoder
| Name | Description |
|---|---|
| Add(String) |
Lägger till en sträng i slutet av StringCollection. |
| AddRange(String[]) |
Kopierar elementen i en strängmatris till slutet av StringCollection. |
| Clear() |
Tar bort alla strängar från StringCollection. |
| Contains(String) |
Avgör om den angivna strängen StringCollectionfinns i . |
| CopyTo(String[], Int32) |
Kopierar hela StringCollection värden till en endimensionell matris med strängar, med början vid det angivna indexet för målmatrisen. |
| Equals(Object) |
Avgör om det angivna objektet är lika med det aktuella objektet. (Ärvd från Object) |
| GetEnumerator() |
Returnerar en StringEnumerator som itererar via StringCollection. |
| GetHashCode() |
Fungerar som standard-hash-funktion. (Ärvd från Object) |
| GetType() |
Hämtar den aktuella instansen Type . (Ärvd från Object) |
| IndexOf(String) |
Söker efter den angivna strängen och returnerar det nollbaserade indexet för den första förekomsten i StringCollection. |
| Insert(Int32, String) |
Infogar en sträng i StringCollection det angivna indexet. |
| MemberwiseClone() |
Skapar en ytlig kopia av den aktuella Object. (Ärvd från Object) |
| Remove(String) |
Tar bort den första förekomsten av en specifik sträng från StringCollection. |
| RemoveAt(Int32) |
Tar bort strängen vid det angivna indexet för StringCollection. |
| ToString() |
Returnerar en sträng som representerar det aktuella objektet. (Ärvd från Object) |
Explicita gränssnittsimplementeringar
| Name | Description |
|---|---|
| ICollection.CopyTo(Array, Int32) |
Kopierar hela StringCollection till en kompatibel endimensionell Array, med början vid det angivna indexet för målmatrisen. |
| IEnumerable.GetEnumerator() |
Returnerar en IEnumerator som itererar via StringCollection. |
| IList.Add(Object) |
Lägger till ett objekt i slutet av StringCollection. |
| IList.Contains(Object) |
Avgör om ett element finns i StringCollection. |
| IList.IndexOf(Object) |
Söker efter den angivna Object och returnerar det nollbaserade indexet för den första förekomsten i hela StringCollection. |
| IList.Insert(Int32, Object) |
Infogar ett element i StringCollection det angivna indexet. |
| IList.IsFixedSize |
Hämtar ett värde som anger om StringCollection objektet har en fast storlek. |
| IList.IsReadOnly |
Hämtar ett värde som anger om objektet StringCollection är skrivskyddat. |
| IList.Item[Int32] |
Hämtar eller anger elementet vid det angivna indexet. |
| IList.Remove(Object) |
Tar bort den första förekomsten av ett specifikt objekt från StringCollection. |
Tilläggsmetoder
| Name | Description |
|---|---|
| AsParallel(IEnumerable) |
Möjliggör parallellisering av en fråga. |
| AsQueryable(IEnumerable) |
Konverterar en IEnumerable till en IQueryable. |
| Cast<TResult>(IEnumerable) |
Omvandlar elementen i en IEnumerable till den angivna typen. |
| OfType<TResult>(IEnumerable) |
Filtrerar elementen i en IEnumerable baserat på en angiven typ. |
Gäller för
Trådsäkerhet
Offentliga statiska (Shared i Visual Basic) medlemmar av den här typen är trådsäkra. Vilka som helst instansmedlemmar garanteras inte att vara trådsäkra.
Den här implementeringen tillhandahåller ingen synkroniserad (trådsäker) omslutning för en StringCollection, men härledda klasser kan skapa sina egna synkroniserade versioner av StringCollection egenskapen SyncRoot .
Att räkna upp genom en samling är i sig inte en trådsäker procedur. Även när en samling synkroniseras kan andra trådar fortfarande ändra samlingen, vilket gör att uppräknaren genererar ett undantag. För att garantera trådsäkerheten under uppräkningen kan du antingen låsa samlingen under hela uppräkningen eller fånga undantagen från ändringar som gjorts av andra trådar.