Home  >  Article  >  Backend Development  >  Detailed explanation of the Console.Read() method in C#

Detailed explanation of the Console.Read() method in C#

藏色散人
藏色散人Original
2019-03-25 13:21:329075browse

Console.Read() method is used to read the next character from the standard input stream. This method basically prevents the user from returning when they type some input characters. It will terminate once the user presses ENTER.

Detailed explanation of the Console.Read() method in C#

Syntax:

public static int Read();

Return value: Returns the next character in the input stream, or a negative number (- if there is currently no character to read) 1).

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

The following program illustrates the use of the above method:

Example 1:

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

Output:

Detailed explanation of the Console.Read() method in C#

Example 2:

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

Output:

Detailed explanation of the Console.Read() method in C#

Related recommendations:《 C Tutorial

This article is about the detailed explanation of the Console.Read() method in C#. I hope it will be helpful to friends in need!

The above is the detailed content of Detailed explanation of the Console.Read() method in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn