Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

How is Console.ReadLine different from a variable?

As I have understood it, Console.ReadLine stores input from the user, which then can be used fx with Console.WriteLine which then writes it out. Please correct me if I'm wrong. But how is this different from a variable?

1 Answer

Dominik Huber
Dominik Huber
4,631 Points

Console.ReadLine doesn't actually stores the input from the user, it just returns the input from the user as a String. I don't know if you learned about Methods yet but ReadLine is a Method from the Console class.

And this method returns a string. So you need a variable to hold the value it returns, otherwise it's gone. You have to save this string either in a variable or print it out directly. 3 quick examples:

Console.ReadLine(); --> The input from the user is gone. (though often used to halt the program and continues after the user has hit enter key.

string userInput = Console.ReadLine() --> Input is stored in userInput variable

Console.WriteLine(Console.ReadLine()); --> The input is gone but it get's printed out on the console. But it can't be used later because it's not stored anywhere.

Thanks. Makes sense.