C# .Net – Reading input from Console

Console.ReadLine():

  • It is used to read input from the Console.
  • ReadLine() method always returns the input in String format only.
  • We need to convert that String into corresponding type.

Reading String:

using System;
public class Program
{
	public static void Main()
	{
		Console.Write("Enter your name : ");
		String name = Console.ReadLine();
		Console.WriteLine("Hello " + name);
	}
}

Reading integer:

using System;
public class Program
{
	public static void Main()
	{
		Console.Write("Enter an integer : ");
		String s = Console.ReadLine();
		int n = Convert.ToInt32(s);
		Console.WriteLine("Input number is : " + n);
	}
}

Reading int, double and boolean:

using System;
public class Program
{
	public static void Main()
	{
		Console.Write("Enter an integer : ");
		int n = Convert.ToInt32(Console.ReadLine());
		
		Console.Write("Enter double : ");
		double d = Convert.ToDouble(Console.ReadLine());
				
		Console.Write("Enter boolean : ");
		bool b = Convert.ToBoolean(Console.ReadLine());
		
		Console.WriteLine("Integer value is : " + n);
		Console.WriteLine("Double value is : " + d);
		Console.WriteLine("Boolean value is : " + b);
		
	}
}

Reading Character:

using System;
public class Program
{
	public static void Main()
	{
		Console.Write("Enter character : ");
		char ch = Console.ReadLine()[0];
		Console.WriteLine("Character is : " + ch);	
	}
}

Reading Employee details:

using System;
public class Program
{
	public static void Main()
	{
		Console.WriteLine("Enter Emp details(id, name, gender, salary, married : ");
		int id = Convert.ToInt32(Console.ReadLine());
		String name = Console.ReadLine();
		char gender = Console.ReadLine()[0];
		double salary = Convert.ToDouble(Console.ReadLine());
		bool married = Convert.ToBoolean(Console.ReadLine());
		
		Console.WriteLine("Details : " + id + " , " + name + " , " + gender + " , " + salary + " , " + married);
	}
}
Scroll to Top