Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

เมธอด CharEnumerator.Reset() ใน C#

เมธอด CharEnumerator.Reset() ใน C# ตั้งค่าดัชนีกลับไปที่ตำแหน่งก่อนตัวอักษรแรกของสตริงที่กำลังนับ

ไวยากรณ์

public void Reset();

ตัวอย่าง: ใช้ Reset() เพื่อวนลูปซ้ำ

using System;

public class Demo
{
    public static void Main()
    {
        string strNum = "This is it!";
        CharEnumerator ch = strNum.GetEnumerator();
        
        Console.WriteLine("HashCode = " + ch.GetHashCode());
        Console.WriteLine("Get the Type = " + ch.GetType());
        
        while (ch.MoveNext())
            Console.Write(ch.Current + " ");
        
        ch.Reset();
        Console.WriteLine();
        
        while (ch.MoveNext())
            Console.Write(ch.Current);
    }
}

ผลลัพธ์

HashCode = 65311716
Get the Type = System.CharEnumerator
T h i s i s i t !
This is it!

การใช้งาน: ใช้ Reset() เพื่อเริ่มนับใหม่จากต้นสตริงหลังจากวนลูปจบแล้ว