Dùng ReadLine() đọc input từ console. Nhận chuỗi nên cần chuyển đổi.
Ví dụ đọc số nguyên:
string val;
Console.Write("Nhập số nguyên: ");
val = Console.ReadLine();
int a = Convert.ToInt32(val);
Console.WriteLine("Input của bạn: {0}", a);
Ví dụ
using System;
using System.Collections.Generic;
class Demo {
static void Main() {
string val;
Console.Write("Nhập số nguyên: ");
val = Console.ReadLine();
int a = Convert.ToInt32(val);
Console.WriteLine("Input của bạn: {0}", a);
}
}
Kết quả:
Nhập số nguyên: 5 Input của bạn: 5