Console.Read メソッド
アセンブリ: mscorlib (mscorlib.dll 内)



Read メソッドは、ユーザーが文字を入力している間、制御から戻らず待機し、Enter キーが押された時点で終了します。Enter キーを押すと、プラットフォーム依存の行終端文字シーケンスが入力内容に追加されます。たとえば、Windows では、復帰と改行から成るシーケンスが追加されます。繰り返し Read メソッドを呼び出した場合、入力内容が 1 文字ずつ取得されます。最後の文字を取得すると、Read は再び待機状態に入り、同じサイクルが繰り返されます。
プロパティ値が -1 になるのは、Control 修飾子キーと Z コンソール キー (Ctrl + Z) を同時に押してファイルの終端を示す信号を送ったか、ファイルの終端条件に相当するキー (Windows の場合は F6 ファンクション キー) を押した、あるいは、入力ストリームをテキスト ファイルなどのソースにリダイレクトした (この場合はテキスト ファイルに実際のファイル終端文字が含まれるため) 場合だけである点に注意してください。
Read メソッドを使用する場合、同じ処理を ReadLine メソッドで、または、KeyAvailable プロパティと ReadKey メソッドを組み合わせて実現できないか検討することをお勧めします。

' This example demonstrates the Console.Read() method. Imports System Imports Microsoft.VisualBasic Class Sample Public Shared Sub Main() Dim m1 As String = _ vbCrLf & _ "Type a string of text then press Enter. " & _ "Type '+' anywhere in the text to quit:" & _ vbCrLf Dim m2 As String = "Character '{0}' is hexadecimal 0x{1:x4}." Dim m3 As String = "Character is hexadecimal 0x{0:x4}." Dim ch As Char Dim x As Integer ' Console.WriteLine(m1) Do x = Console.Read() Try ch = Convert.ToChar(x) If Char.IsWhiteSpace(ch) Then Console.WriteLine(m3, x) If ch = vbLf Then Console.WriteLine(m1) End If Else Console.WriteLine(m2, ch, x) End If Catch e As OverflowException Console.WriteLine("{0} Value read = {1}.", e.Message, x) ch = Char.MinValue Console.WriteLine(m1) End Try Loop While ch <> "+"c End Sub 'Main End Class 'Sample ' 'This example produces the following results: ' 'Type a string of text then press Enter. Type '+' anywhere in the text to quit: ' 'The quick brown fox. 'Character 'T' is hexadecimal 0x0054. 'Character 'h' is hexadecimal 0x0068. 'Character 'e' is hexadecimal 0x0065. 'Character is hexadecimal 0x0020. 'Character 'q' is hexadecimal 0x0071. 'Character 'u' is hexadecimal 0x0075. 'Character 'i' is hexadecimal 0x0069. 'Character 'c' is hexadecimal 0x0063. 'Character 'k' is hexadecimal 0x006b. 'Character is hexadecimal 0x0020. 'Character 'b' is hexadecimal 0x0062. 'Character 'r' is hexadecimal 0x0072. 'Character 'o' is hexadecimal 0x006f. 'Character 'w' is hexadecimal 0x0077. 'Character 'n' is hexadecimal 0x006e. 'Character is hexadecimal 0x0020. 'Character 'f' is hexadecimal 0x0066. 'Character 'o' is hexadecimal 0x006f. 'Character 'x' is hexadecimal 0x0078. 'Character '.' is hexadecimal 0x002e. 'Character is hexadecimal 0x000d. 'Character is hexadecimal 0x000a. ' 'Type a string of text then press Enter. Type '+' anywhere in the text to quit: ' '^Z 'Value was either too large or too small for a character. Value read = -1. ' 'Type a string of text then press Enter. Type '+' anywhere in the text to quit: ' '+ 'Character '+' is hexadecimal 0x002b. '
// This example demonstrates the Console.Read() method. using System; class Sample { public static void Main() { string m1 = "\nType a string of text then press Enter. " + "Type '+' anywhere in the text to quit:\n"; string m2 = "Character '{0}' is hexadecimal 0x{1:x4}."; string m3 = "Character is hexadecimal 0x{0:x4}."; char ch; int x; // Console.WriteLine(m1); do { x = Console.Read(); try { ch = Convert.ToChar(x); if (Char.IsWhiteSpace(ch)) { Console.WriteLine(m3, x); if (ch == 0x0a) Console.WriteLine(m1); } else Console.WriteLine(m2, ch, x); } catch (OverflowException e) { Console.WriteLine("{0} Value read = {1}.", e.Message, x); ch = Char.MinValue; Console.WriteLine(m1); } } while (ch != '+'); } } /* This example produces the following results: Type a string of text then press Enter. Type '+' anywhere in the text to quit: The quick brown fox. Character 'T' is hexadecimal 0x0054. Character 'h' is hexadecimal 0x0068. Character 'e' is hexadecimal 0x0065. Character is hexadecimal 0x0020. Character 'q' is hexadecimal 0x0071. Character 'u' is hexadecimal 0x0075. Character 'i' is hexadecimal 0x0069. Character 'c' is hexadecimal 0x0063. Character 'k' is hexadecimal 0x006b. Character is hexadecimal 0x0020. Character 'b' is hexadecimal 0x0062. Character 'r' is hexadecimal 0x0072. Character 'o' is hexadecimal 0x006f. Character 'w' is hexadecimal 0x0077. Character 'n' is hexadecimal 0x006e. Character is hexadecimal 0x0020. Character 'f' is hexadecimal 0x0066. Character 'o' is hexadecimal 0x006f. Character 'x' is hexadecimal 0x0078. Character '.' is hexadecimal 0x002e. Character is hexadecimal 0x000d. Character is hexadecimal 0x000a. Type a string of text then press Enter. Type '+' anywhere in the text to quit: ^Z Value was either too large or too small for a character. Value read = -1. Type a string of text then press Enter. Type '+' anywhere in the text to quit: + Character '+' is hexadecimal 0x002b. */
// This example demonstrates the Console.Read() method. using namespace System; int main() { String^ m1 = "\nType a string of text then press Enter. " "Type '+' anywhere in the text to quit:\n"; String^ m2 = "Character '{0}' is hexadecimal 0x{1:x4}."; String^ m3 = "Character is hexadecimal 0x{0:x4}."; Char ch; int x; // Console::WriteLine( m1 ); do { x = Console::Read(); try { ch = Convert::ToChar( x ); if ( Char::IsWhiteSpace( ch ) ) { Console::WriteLine( m3, x ); if ( ch == 0x0a ) Console::WriteLine( m1 ); } else Console::WriteLine( m2, ch, x ); } catch ( OverflowException^ e ) { Console::WriteLine( "{0} Value read = {1}.", e->Message, x ); ch = Char::MinValue; Console::WriteLine( m1 ); } } while ( ch != '+' ); } /* This example produces the following results: Type a string of text then press Enter. Type '+' anywhere in the text to quit: The quick brown fox. Character 'T' is hexadecimal 0x0054. Character 'h' is hexadecimal 0x0068. Character 'e' is hexadecimal 0x0065. Character is hexadecimal 0x0020. Character 'q' is hexadecimal 0x0071. Character 'u' is hexadecimal 0x0075. Character 'i' is hexadecimal 0x0069. Character 'c' is hexadecimal 0x0063. Character 'k' is hexadecimal 0x006b. Character is hexadecimal 0x0020. Character 'b' is hexadecimal 0x0062. Character 'r' is hexadecimal 0x0072. Character 'o' is hexadecimal 0x006f. Character 'w' is hexadecimal 0x0077. Character 'n' is hexadecimal 0x006e. Character is hexadecimal 0x0020. Character 'f' is hexadecimal 0x0066. Character 'o' is hexadecimal 0x006f. Character 'x' is hexadecimal 0x0078. Character '.' is hexadecimal 0x002e. Character is hexadecimal 0x000d. Character is hexadecimal 0x000a. Type a string of text then press Enter. Type '+' anywhere in the text to quit: ^Z Value was either too large or too small for a character. Value read = -1. Type a string of text then press Enter. Type '+' anywhere in the text to quit: + Character '+' is hexadecimal 0x002b. */
// This example demonstrates the Console.Read() method. import System.*; class Sample { public static void main(String[] args) { String m1 = "\nType a string of text then press Enter. " + "Type '+' anywhere in the text to quit:\n"; String m2 = "Character '{0}' is hexadecimal 0x{1}."; String m3 = "Character is hexadecimal 0x{0}."; char ch; int x; // Console.WriteLine(m1); do { x = Console.Read(); try { ch = Convert.ToChar(x); if (Char.IsWhiteSpace(ch)) { Console.WriteLine(m3, ((Int32)x).ToString("x4")); if (ch == 0xA) { Console.WriteLine(m1); } } else { Console.WriteLine(m2, ((Char)ch).ToString(), ((Int32)x).ToString("x4")); } } catch (OverflowException e) { Console.WriteLine("{0} Value read = {1}.", e.get_Message(), System.Convert.ToString(x)); ch = Char.MinValue; Console.WriteLine(m1); } } while (ch != '+'); } //main } //Sample /* This example produces the following results: Type a String of text then press Enter. Type '+' anywhere in the text to quit: The quick brown fox. Character 'T' is hexadecimal 0x0054. Character 'h' is hexadecimal 0x0068. Character 'e' is hexadecimal 0x0065. Character is hexadecimal 0x0020. Character 'q' is hexadecimal 0x0071. Character 'u' is hexadecimal 0x0075. Character 'i' is hexadecimal 0x0069. Character 'c' is hexadecimal 0x0063. Character 'k' is hexadecimal 0x006b. Character is hexadecimal 0x0020. Character 'b' is hexadecimal 0x0062. Character 'r' is hexadecimal 0x0072. Character 'o' is hexadecimal 0x006f. Character 'w' is hexadecimal 0x0077. Character 'n' is hexadecimal 0x006e. Character is hexadecimal 0x0020. Character 'f' is hexadecimal 0x0066. Character 'o' is hexadecimal 0x006f. Character 'x' is hexadecimal 0x0078. Character '.' is hexadecimal 0x002e. Character is hexadecimal 0x000d. Character is hexadecimal 0x000a. Type a String of text then press Enter. Type '+' anywhere in the text to quit: ^Z Value was either too large or too small for a character. Value read = -1. Type a String of text then press Enter. Type '+' anywhere in the text to quit: + Character '+' is hexadecimal 0x002b. */

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からConsole.Read メソッドを検索する場合は、下記のリンクをクリックしてください。

- Console.Read メソッドのページへのリンク