C#教程
C#控制语句
C#函数
C#数组
C#面向对象
C#命名空间和异常
C#文件IO
C#集合
C#多线程
C#其它

C# ReadLine()

C# ReadLine()方法

Console.WriteLine()是C#中用来打印单行的整个语句并传输的方法控制到控制台的下一行。与 Console.WriteLine() 类似,ReadLine() 方法用于从用户读取整行字符串或语句值,直到按下 Enter 键以将控制权转移到下一行。在本节中,我们将详细了解ReadLine()、Read() 和Readkey() 方法。
C# ReadLine() 方法

Console.ReadLine() 方法

在 C#,ReadLine() 方法是一种常用的方法或函数,用于从用户获取输入直到按下回车键。换句话说,它是一种从标准输入流中读取每一行字符串或值的方法。
它是Console 类(系统命名空间)的预定义方法。 Console.ReadLine() 方法从流输出设备(控制台)读取并仅返回字符串,直到找到换行符为止。如果我们想从用户那里读取一个字符或数值,我们需要将字符串转换成合适的数据集。
语法
public static string ReadLine ();
该方法抛出以下异常:
IOException: 如果出现 I/O 错误,就会发生这种情况。 OutOfMemoryException: 如果内存不足,无法分配缓冲区以返回字符串,则会发生这种情况。 ArgumentOutOfRangeException: 如果下一行的字符数大于MaxValue,就会发生这种情况。
示例 1: 让我们编写一个程序,使用 ReadLine() 方法从用户那里获取输入。
Program.cs
using System; // Define the System package
namespace ConsoleApp3 // Project name or Folder
{
    class Program 
    {
        static void Main(string[] args) // Defining the main function
        {
            string name; // string variable name
            Console.WriteLine("Hello, what is your name?"); 
            name = Console.ReadLine(); // takes an input from the user
            Console.WriteLine("Hi! "+ name + " Welcome to the lidihuo"); // print the output
        }
    }
}
输出
C# ReadLine() Method
示例 2: 使用 C# 中的 ReadLine() 函数编写一个程序来打印用户的名字和姓氏。
Program2.cs
using System;
namespace ConsoleApp3
{
    class Program2
    {
        static void Main(string[] args)
        {
            string fname, lname; // string variables
            Console.Write("Please, Enter your first Name : ");
            fname = Console.ReadLine(); // takes the first name from the user
            // ReadLine() is a method of Console class to read a line from the standard input stream
            Console.Write("Please, Enter Your Last Name : ");
            lname = Console.ReadLine(); // takes the second name from the user
            Console.WriteLine("Your Full Name is : " + fname + " " + lname);
        }
    }
}
输出
C# ReadLine() Method

Read() 方法

C# 中的 Read() 方法用于从用户读取单个字符。它与 ReadLine() 方法不同,因为 readLine() 方法接收用户的每一行输入,直到该行完成并将控制转移到下一个语句读取
让我们写一个程序来理解在C#中使用Read()方法打印一个字符。
Program4.cs
using System; // Define the System package
namespace ConsoleApp3
{
    class Program4 
    {
        static void Main(string[] args) 
        {
            char ch;
            Console.Write("Enter the characters "); // Cosole.Write() print the same line statement.
            ch = Convert.ToChar(Console.Read()); // Read a single character from the user.
            Console.WriteLine("You have entered the character " + ch); //print the complete line
        }
    }
}
输出
C# ReadLine() Method

ReadKey()

ReadKey() 方法用于获取下一个字符,或者用户按任意键退出程序。它保持屏幕直到用户按下键盘上的任意键。按下的键将显示在控制台上。
Program5.cs
using System; // Define the System package
namespace ConsoleApp3
{
    class Program5 
    {
        static void Main(string[] args) 
        {     
             DateTime dt = DateTime.Now; // DateTime.Now() print the current time
             Console.WriteLine(" The Current Date and Time is : " + dt);
             Console.Write("Press any key or Enter to exit from the Console Screen");
             Console.ReadKey(); // enter any key to exit from the console screen.
            }
    }
}
输出
C# ReadLine() Method
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4