Saturday, 15 November 2014

C# - Input/Output

Take input from users and printing output in console

Lets say, you're going for an interview and the interviewer asks you the following questions.

Q1: What's your name?

You answer, my name is 'Xyz'

So, in C# how to print some text on screen? On console you can do the following.

Use Console.WriteLine();

To print something on screen. Type in your Main() function:

- Console.WriteLine("What is your name");

WriteLine() function takes 1 parameter as string data-type.

 Now, to take user's input we use Console.ReadLine() but it must be saved somewhere so we can use it. We must have a reference to it in our memory. We do it like this:

- String name = Console.ReadLine();

This will create a variable 'name' of data-type 'String' and assign it a value returned by 'Console.ReadLine()'

ReadLine() doesn't take any parameter, it fetches input from a source, in our case Console.

No comments:

Post a Comment