Dieser Artikel wurde maschinell übersetzt. Wenn Sie die englische Version des Artikels anzeigen möchten, aktivieren Sie das Kontrollkästchen Englisch. Sie können den englischen Text auch in einem Popupfenster anzeigen, indem Sie den Mauszeiger über den Text bewegen.
Übersetzung
Englisch

Console.Read-Methode: ()

 

Veröffentlicht: Oktober 2016

Liest das nächste Zeichen aus dem Standardeingabestream.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

[HostProtectionAttribute(SecurityAction.LinkDemand, UI = true)]
public static int Read()

Rückgabewert

Type: System.Int32

Das nächste Zeichen aus dem Eingabestream bzw. -1, wenn derzeit keine weiteren Zeichen gelesen werden können.

Exception Condition
IOException

E/A-Fehler

The M:System.Console.Read method blocks its return while you type input characters; it terminates when you press the F:System.ConsoleKey.Enter key. Pressing Enter appends a platform-dependent line termination sequence to your input (for example, Windows appends a carriage return-linefeed sequence). Subsequent calls to the M:System.Console.Read method retrieve your input one character at a time. After the final character is retrieved, M:System.Console.Read blocks its return again and the cycle repeats.

System_CAPS_importantWichtig

The M:System.Console.ReadLine method, or the P:System.Console.KeyAvailable property and M:System.Console.ReadKey method are preferable to using the M:System.Console.Read method.

Note that the method does not return -1 unless you perform one of the following actions:

  • Simultaneously press the F:System.ConsoleModifiers.Control modifier key and F:System.ConsoleKey.Z console key (Ctrl+Z), which signals the end-of-file condition.

  • Press an equivalent key that signals the end-of-file condition, such as the F6 function key in Windows.

  • Redirect the input stream to a source, such as a text file, that has an actual end-of-file character.

The following example demonstrates the M:System.Console.Read method.

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

*/

.NET Framework
Verfügbar seit 1.1
Zurück zum Anfang
Anzeigen: