Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


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.