Open In App

C# | CharEnumerator.Dispose() Method

Improve
Improve
Like Article
Like
Save
Share
Report

This method is used to releases all resources used by the current instance of the CharEnumerator class. The Dispose() method leaves the CharEnumerator in an unusable state. So, this method should be called when a user finished their working with the CharEnumerator.

Syntax:

public void Dispose ();

Return Value: This method returns nothing.

Below are the programs to illustrate the use of CharEnumerator.Dispose() Method:

Example 1:




// C# program to illustrate the
// use of CharEnumerator.Dispose()
// Method
using System;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
        // Initialize a string object
        string str = "GeeksforGeeks is Awesome!";
  
        // Instantiate a CharEnumerator object
        CharEnumerator chEnum = str.GetEnumerator();
  
        // Print the string using CharEnumerator
        while (chEnum.MoveNext())
            Console.Write(chEnum.Current);
  
        // CharEnumerator object is disposed
        chEnum.Dispose();
  
        // Try printing current character
        // using CharEnumerator
        try
        {
  
            Console.Write(chEnum.Current);
        }
  
        catch (Exception e) 
        {
            Console.Write("\nShows " + e.GetType() + " as"+
                 " the CharEnumerator object is disposed.");
        }
    }
}


Output:

GeeksforGeeks is Awesome!
Shows System.NullReferenceException as the CharEnumerator object is disposed.

Example 2:




// C# program to illustrate the
// use of CharEnumerator.Dispose()
// Method
using System;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
        // Initialize a string object
        string str = "GeeksforGeeks is Awesome";
  
        // Instantiate a CharEnumerator object
        CharEnumerator chEnum = str.GetEnumerator();
  
        while (chEnum.MoveNext()) 
        {
            // Print current character
            Console.Write(chEnum.Current);
  
            // Dispose the CharEnumerator object
            // when space is encountered
            if (chEnum.Current == ' ')
            {
                Console.WriteLine(".\nDispose CharEnumerator object and "+
                                        "try to print current character.");
                chEnum.Dispose();
                break;
            }
        }
   
        // Try printing current character
        // using CharEnumerator
        try
        {
            Console.Write(chEnum.Current);
        }
          
        catch (Exception e) 
        {
            Console.Write("Shows " + e.GetType() + " as the"+
                      " CharEnumerator object is disposed.");
        }
    }
}


Output:

GeeksforGeeks .
Dispose CharEnumerator object and try to print current character.
Shows System.NullReferenceException as the CharEnumerator object is disposed.

Reference:



Last Updated : 30 Apr, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads