C# | CharEnumerator.Dispose() Method

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.");
        }
    }
}

chevron_right


Output:

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

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.");
        }
    }
}

chevron_right


Output:

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

Reference:



My Personal Notes arrow_drop_up

Coder Machine Learner Social Activist Vocalist

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.