英語で読む

次の方法で共有


Console.Read メソッド

定義

標準入力ストリームから次の文字を読み取ります。

C#
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static int Read();
C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static int Read();
C#
public static int Read();

戻り値

入力ストリームの次の文字。または次の文字がない場合は -1。

属性

例外

I/O エラーが発生しました。

Readメソッドの例を次に示します。

C#
// 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.

*/

注釈

メソッドは Read 、入力文字を入力するときに戻り値をブロックします。キーを押 Enter すと終了します。 Enter キーを押すと、プラットフォーム依存の行終端シーケンスが入力に追加されます (たとえば、Windows では復帰ラインフィード シーケンスが追加されます)。 メソッドの後続の呼び出しでは Read 、一度に 1 文字ずつ入力が取得されます。 最後の文字が取得されたら、 Read その戻り値をもう一度ブロックし、サイクルが繰り返されます。

重要

方法、又は ReadLine 上記 KeyAvailable の特性及び ReadKey 方法は、前記方法を用いる方が Read 好ましい。

次のいずれかのアクションを実行しない限り、 メソッドは -1 を返しません。

  • 修飾子キーとZコンソール キー (Ctrl + Z キー) を同時に押Controlすと、ファイルの終了条件が通知されます。 Windows を使用している場合は、コンソール キーも押す Enter 必要があります。

  • Windows の F6 ファンクション キーなど、ファイルの終了条件を通知する同等のキーを押します。

  • 入力ストリームを、ファイルの実際の終わり文字を持つテキスト ファイルなどのソースにリダイレクトします。

適用対象

製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

こちらもご覧ください