Console.Read() Method in C#

Console.Read() Method is used to read the next character from the standard input stream. This method basically blocks its return when the user types some input characters. As soon as the user press ENTER key it terminates.

Syntax: public static int Read ();

Return Value: It returns the next character from the input stream, or a negative one (-1) if there are currently no more characters to be read.



Exception: This method will give IOException if an I/O error occurred.

Below programs illustrate the use of above-discussed method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the use
// of Console.Read Method
using System;
  
namespace GFG {
  
class Program {
  
    static void Main(string[] args)
    {
          
        int x;
        Console.WriteLine("Enter your Character to get Decimal number");
  
        // using the method
        x = Console.Read();
        Console.WriteLine(x);
    }
}
}

chevron_right


Output:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the use
// of Console.Read Method
using System;
  
namespace GFG {
  
class Program {
  
    static void Main(string[] args)
    {
        // Write to console window.
  
        int x;
        Console.WriteLine("Enter your Character to get Decimal number");
        x = Console.Read();
        Console.WriteLine(x);
  
        // Converting the decimal into character.
        Console.WriteLine(Convert.ToChar(x));
    }
}
}

chevron_right


Output:

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.