C#中的CharEnumerator.Dispose()方法

C#中的CharEnumerator.Dispose()方法用于释放CharEnumerator类的当前实例使用的所有资源。

语法

public void Dispose ();

现在让我们看一个实现CharEnumerator.Dispose()方法的示例-

示例

using System;
public class Demo {
   public static void Main(){
      string strNum = "356";
      CharEnumerator ch = strNum.GetEnumerator();
      while (ch.MoveNext())
         Console.Write(ch.Current + " ");
      //处置
      ch.Dispose();
      // this will show an error since we处置 the object above
      //Console.WriteLine(ch.Current); 
   }
}

输出结果

这将产生以下输出-

3 5 6