Console.OpenStandardInput方法用于获取标准输入流。下面列出了C#中提供的OpenStandardInput方法的两个重载:
- OpenStandardInput()方法
- OpenStandardInput(int32)方法
OpenStandardInput()方法
它用于获取标准输入流。创建Stream类的对象,并使用此方法,用户可以将Input引用提供给该对象。它创建一个缓冲区,用于接收用户输入。通过SetIn方法更改标准输入流后,也可以使用此方法重新获取标准输入流。
句法:
public static System.IO.Stream OpenStandardInput ();
例子:
// C# program to illustrate the
// OpenStandardInput() Method
using System;
using System.Text;
using System.IO;
class GFG {
public static void Main()
{
// Stream Object declared and
// OpenStandardInput method is used
Stream inputStream = Console.OpenStandardInput();
byte[] bytes = new byte[50];
int outputLength = inputStream.Read(bytes, 0, 50);
char[] chars = Encoding.UTF7.GetChars(bytes, 0, outputLength);
Console.WriteLine(new string(chars));
}
}
输出:
OpenStandardInput(Int32)方法
它还用于获取设置为指定缓冲区大小的标准输入流。在此方法中传递的值确定缓冲区的大小。通过SetIn方法更改标准输入流后,也可以使用此方法重新获取标准输入流。
Syntax: public static System.IO.Stream OpenStandardInput (int bufferSize);
Parameters:
buffersize: It is the internal stream buffer size.
Return Value: It returns the standard input stream.
Exception: This method will give ArgumentOutOfRangeException if the buffersize is less than or equal to zero.
// C# program to illustrate the
// OpenStandardInput(Int32) Method
using System;
using System.Text;
using System.IO;
class GFG {
// Main Method
public static void Main()
{
// Using the Method
Stream inputStream = Console.OpenStandardInput(100);
byte[] bytes = new byte[100];
int outputLength = inputStream.Read(bytes, 0, 100);
char[] chars = Encoding.UTF7.GetChars(bytes, 0, outputLength);
Console.WriteLine(new string(chars));
}
}
输出:
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.console.openstandardinput?view=netframework-4.7.2