How to read a value from Console?


This article explains how to get an input from the console. If you have used C++ then cin and cout are the functions for read and write to the console. 

Type this code in a text editor and save file as read.cs.

using System;
class ReadStr
{
public static void Main()
{
Console.Write("Enter your name:");
string szName = Console.ReadLine();
Console.WriteLine(" Hey "+ szName);
}
}

Now compile it from command line with syntax cs read.cs. It will create an exe, read.exe in same dir where your read.cs file is. Run the exe.

NOTE: I like to use my VC++ editor to create a text file and save it as .cs extension.

Up Next
    Ebook Download
    View all
    Learn
    View all