Open In App

Console.Read() Method in C#

Last Updated : 28 Feb, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report

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:




// C# program to illustrate the use
// of Console.Read Method
usingSystem;
  
namespaceGFG {
  
classProgram {
  
    staticvoidMain(string[] args)
    {
          
        intx;
        Console.WriteLine("Enter your Character to get Decimal number");
  
        // using the method
        x = Console.Read();
        Console.WriteLine(x);
    }
}
}

Output:

Example 2:




// C# program to illustrate the use
// of Console.Read Method
usingSystem;
  
namespaceGFG {
  
classProgram {
  
    staticvoidMain(string[] args)
    {
        // Write to console window.
  
        intx;
        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));
    }
}
}

Output:

Reference:



Next Article

Similar Reads

close