ใช้เมธอด ReadLine() อ่าน Input จาก Console รับค่าเป็น String ต้อง Convert ให้เป็น Type ที่ต้องการ
ตัวอย่าง: อ่าน Integer
string val;
Console.Write("Enter integer: ");
val = Console.ReadLine();
int a = Convert.ToInt32(val);
Console.WriteLine("Your input: {0}", a);
ตัวอย่างสมบูรณ์
using System;
using System.Collections.Generic;
class Demo
{
static void Main()
{
string val;
Console.Write("Enter Integer: ");
val = Console.ReadLine();
int a = Convert.ToInt32(val);
Console.WriteLine("Your input: {0}", a);
}
}
Output
Enter Integer: 5 Your input: 5