CharEnumerator 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.
Stöder iterering över ett String objekt och läsning av dess enskilda tecken. Det går inte att ärva den här klassen.
public ref class CharEnumerator sealed : ICloneable, System::Collections::Generic::IEnumerator<char>
public ref class CharEnumerator sealed : ICloneable, System::Collections::IEnumerator
public sealed class CharEnumerator : ICloneable, System.Collections.Generic.IEnumerator<char>
[System.Serializable]
public sealed class CharEnumerator : ICloneable, System.Collections.IEnumerator
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class CharEnumerator : ICloneable, System.Collections.Generic.IEnumerator<char>
type CharEnumerator = class
interface IEnumerator<char>
interface IEnumerator
interface IDisposable
interface ICloneable
[<System.Serializable>]
type CharEnumerator = class
interface IEnumerator
interface ICloneable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type CharEnumerator = class
interface ICloneable
interface IEnumerator<char>
interface IDisposable
interface IEnumerator
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type CharEnumerator = class
interface ICloneable
interface IEnumerator<char>
interface IEnumerator
interface IDisposable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type CharEnumerator = class
interface IEnumerator
interface ICloneable
interface IEnumerator<char>
interface IDisposable
Public NotInheritable Class CharEnumerator
Implements ICloneable, IEnumerator(Of Char)
Public NotInheritable Class CharEnumerator
Implements ICloneable, IEnumerator
- Arv
-
CharEnumerator
- Attribut
- Implementeringar
Exempel
I följande exempel används CharEnumerator klassen för att räkna upp enskilda tecken i en sträng. Det instansierar ett CharEnumerator objekt genom att anropa String.GetEnumerator metoden, flyttas från ett tecken till ett annat genom att anropa MoveNext metoden och visar det aktuella tecknet genom att hämta värdet för Current egenskapen.
string title = "A Tale of Two Cities";
CharEnumerator chEnum = title.GetEnumerator();
int ctr = 1;
string outputLine1 = null;
string outputLine2 = null;
string outputLine3 = null;
while (chEnum.MoveNext())
{
outputLine1 += ctr < 10 || ctr % 10 != 0 ? " " : (ctr / 10) + " ";
outputLine2 += (ctr % 10) + " ";
outputLine3 += chEnum.Current + " ";
ctr++;
}
Console.WriteLine("The length of the string is {0} characters:",
title.Length);
Console.WriteLine(outputLine1);
Console.WriteLine(outputLine2);
Console.WriteLine(outputLine3);
// The example displays the following output to the console:
// The length of the string is 20 characters:
// 1 2
// 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
// A T a l e o f T w o C i t i e s
let title = "A Tale of Two Cities"
let chEnum = title.GetEnumerator()
printfn $"The length of the string is {title.Length} characters:"
let mutable outputLine1 = ""
let mutable outputLine2 = ""
let mutable outputLine3 = ""
let mutable i = 1
while chEnum.MoveNext() do
outputLine1 <- outputLine1 + if i < 10 || i % 10 <> 0 then " " else $"{i / 10} "
outputLine2 <- outputLine2 + $"{i % 10} ";
outputLine3 <- outputLine3 + $"{chEnum.Current} "
i <- i + 1
printfn "%s" outputLine1
printfn "%s" outputLine2
printfn "%s" outputLine3
// The example displays the following output to the console:
// The length of the string is 20 characters:
// 1 2
// 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
// A T a l e o f T w o C i t i e s
Dim title As String = "A Tale of Two Cities"
Dim chEnum As CharEnumerator = title.GetEnumerator()
Dim ctr As Integer = 1
Dim outputLine1, outputLine2, outputLine3 As String
Do While chEnum.MoveNext()
outputLine1 += CStr(iif(ctr < 10 Or ctr Mod 10 <> 0, " ", CStr(ctr \ 10) + " "))
outputLine2 += (ctr Mod 10)& " "
outputLine3 += chEnum.Current & " "
ctr += 1
Loop
Console.WriteLine("The length of the string is {0} characters:", _
title.Length)
Console.WriteLine(outputLine1)
Console.WriteLine(outputLine2)
Console.WriteLine(outputLine3)
' The example displays the following output to the console:
' The length of the string is 20 characters:
' 1 2
' 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
' A T a l e o f T w o C i t i e s
Observera dock att samma åtgärd kan utföras något mer intuitivt med hjälp av foreach (i C#) eller For Each (i Visual Basic), som följande exempel visar.
string title = "A Tale of Two Cities";
int ctr = 1;
string outputLine1 = null;
string outputLine2 = null;
string outputLine3 = null;
foreach (char ch in title)
{
outputLine1 += ctr < 10 || ctr % 10 != 0 ? " " : (ctr / 10) + " ";
outputLine2 += (ctr % 10) + " ";
outputLine3 += ch + " ";
ctr++;
}
Console.WriteLine("The length of the string is {0} characters:",
title.Length);
Console.WriteLine(outputLine1);
Console.WriteLine(outputLine2);
Console.WriteLine(outputLine3);
// The example displays the following output to the console:
// The length of the string is 20 characters:
// 1 2
// 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
// A T a l e o f T w o C i t i e s
let title = "A Tale of Two Cities"
let chEnum = title.GetEnumerator()
printfn $"The length of the string is {title.Length} characters:"
let mutable outputLine1 = ""
let mutable outputLine2 = ""
let mutable outputLine3 = ""
let mutable i = 1
for ch in title do
outputLine1 <- outputLine1 + if i < 10 || i % 10 <> 0 then " " else $"{i / 10} "
outputLine2 <- outputLine2 + $"{i % 10} ";
outputLine3 <- outputLine3 + $"{ch} "
i <- i + 1
printfn "%s" outputLine1
printfn "%s" outputLine2
printfn "%s" outputLine3
// The example displays the following output to the console:
// The length of the string is 20 characters:
// 1 2
// 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
// A T a l e o f T w o C i t i e s
Dim title As String = "A Tale of Two Cities"
Dim ctr As Integer = 1
Dim outputLine1, outputLine2, outputLine3 As String
For Each ch As Char In title
outputLine1 += CStr(iif(ctr < 10 Or ctr Mod 10 <> 0, " ", CStr(ctr \ 10) + " "))
outputLine2 += (ctr Mod 10)& " "
outputLine3 += ch & " "
ctr += 1
Next
Console.WriteLine("The length of the string is {0} characters:", _
title.Length)
Console.WriteLine(outputLine1)
Console.WriteLine(outputLine2)
Console.WriteLine(outputLine3)
' The example displays the following output to the console:
' The length of the string is 20 characters:
' 1 2
' 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
' A T a l e o f T w o C i t i e s
Kommentarer
A CharEnumerator ger skrivskyddad åtkomst till tecknen i ett refererat String objekt. Till exempel hämtar foreach-instruktionen för programmeringsspråken Microsoft Visual Basic och C#, som itererar genom elementen i en samling, en CharEnumerator från ett String objekt för att iterera genom tecknen i objektet.
Important
Klassen CharEnumerator räknar upp enskilda 16-bitarsinstanser Char . Det tar inte hänsyn till grafer (dvs. ett tecken följt av ett eller flera combiding-tecken) eller surrogatpar (dvs. tecken utanför Unicode Basic Multilingual Plane) som enkla tecken. Använd klassen för en uppräknare som hanterar dessa typer av tecken som en enda enhet StringInfo .
Det finns ingen offentlig konstruktor för CharEnumerator. Anropa i stället ett String objekts GetEnumerator metod för att hämta en CharEnumerator som initieras för att referera till strängen.
A CharEnumerator upprätthåller ett internt index för tecknen i strängen som referenserna CharEnumerator . Indexets tillstånd är ogiltigt när det refererar till en teckenposition logiskt före det första tecknet eller efter det sista tecknet i strängen och giltigt när det refererar till ett tecken i strängen. Indexet initieras till en position logiskt före det första tecknet och anges till en position efter det sista tecknet när iterationen är klar. Ett undantag utlöses om du försöker komma åt ett tecken medan indexet är ogiltigt.
Metoden MoveNext ökar indexet med ett, så de första och efterföljande tecknen används i tur och ordning. Metoden Reset anger indexet till en position logiskt före det första tecknet. Egenskapen Current hämtar det tecken som för närvarande refereras till av index. Metoden Clone skapar en kopia av CharEnumerator.
Note
Flera oberoende instanser av CharEnumerator i en eller flera trådar kan ha åtkomst till en enda instans av String. Den här klassen implementeras för att stödja IEnumerator gränssnittet. Mer information om användningen av en uppräknare finns i avsnittet IEnumerator .
Egenskaper
| Name | Description |
|---|---|
| Current |
Hämtar det aktuella refererade tecknet i strängen som räknas upp av det här CharEnumerator objektet. |
Metoder
| Name | Description |
|---|---|
| Clone() |
Skapar en kopia av det aktuella CharEnumerator objektet. |
| Dispose() |
Släpper alla resurser som används av den aktuella instansen CharEnumerator av klassen. |
| Equals(Object) |
Avgör om det angivna objektet är lika med det aktuella objektet. (Ärvd från Object) |
| GetHashCode() |
Fungerar som standard-hash-funktion. (Ärvd från Object) |
| GetType() |
Hämtar den aktuella instansen Type . (Ärvd från Object) |
| MemberwiseClone() |
Skapar en ytlig kopia av den aktuella Object. (Ärvd från Object) |
| MoveNext() |
Ökar det interna indexet för det aktuella CharEnumerator objektet till nästa tecken i den uppräknade strängen. |
| Reset() |
Initierar indexet till en position logiskt före det första tecknet i den uppräknade strängen. |
| ToString() |
Returnerar en sträng som representerar det aktuella objektet. (Ärvd från Object) |
Explicita gränssnittsimplementeringar
| Name | Description |
|---|---|
| IDisposable.Dispose() |
Släpper alla resurser som används av CharEnumerator klassen. |
| IEnumerator.Current |
Hämtar det aktuella refererade tecknet i strängen som räknas upp av det här CharEnumerator objektet. En beskrivning av den här medlemmen finns i Current. |