📜  C#| CharEnumerator.Reset()方法

📅  最后修改于: 2021-05-29 23:17:25             🧑  作者: Mango

CharEnumerator.Reset方法用于从逻辑上将索引初始化到枚举字符串的第一个字符之前的位置。

句法:

public void Reset ();

下面的程序说明了CharEnumerator.Reset()方法的用法

范例1:

// C# program to illustrate the
// use of CharEnumerator.Reset()
// Method
using System;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
        // Initialize a string object
        string str = "The Sun rises in the East and sets in the West.";
  
        // Instantiate a CharEnumerator object
        CharEnumerator chEnum = str.GetEnumerator();
  
        // Printing the string
        while (chEnum.MoveNext())
            Console.Write(chEnum.Current);
  
        // Reset the CharEnumerator object
        chEnum.Reset();
        Console.WriteLine();
  
        // Printing the string again
        while (chEnum.MoveNext())
            Console.Write(chEnum.Current);
    }
}
输出:
The Sun rises in the East and sets in the West.
The Sun rises in the East and sets in the West.

范例2:

// C# program to illustrate the
// use of CharEnumerator.Reset()
// Method
using System;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
        // Initialize a string object
        string str = "0 1 2 3 4 5 6 7 8 9";
  
        // Instantiate a CharEnumerator object
        CharEnumerator chEnum = str.GetEnumerator();
  
        // Printing the string
        while (chEnum.MoveNext())
            Console.Write(chEnum.Current);
  
        // Reset the CharEnumerator object
        chEnum.Reset();
        Console.WriteLine();
  
        // Printing the string again
        while (chEnum.MoveNext())
            Console.Write(chEnum.Current);
    }
}
输出:
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.charenumerator.reset?view=netframework-4.7.2