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 trialTirth Upadhyay
920 Pointswhy not int instead of string?
we use : string Input = Console.ReadLine();
then we need to convert the string into integer.
why cant we directly use the integer????
2 Answers
Jennifer Nordell
Treehouse TeacherBecause everything coming in from the user from the console is a string. So we have to take what they have put in and change it to an integer. This is not specific to C#. All input from the user (in most languages I know) have what's coming in as strings.
edited for additional note
While you could try to parse the integer from their input when originally assigning to the variable, any incorrect input from the user would cause an unhandled exception and your program would crash. This (imo) would not be the best solution.
Gi Devs
12,171 Pointswe could in theory do it all in one line but for starting perposes it is better to split it up. I like to experiment a bit and work thing s out on my own and I came up with this
int newInput = System.Int32.Parse(System.Console.ReadLine());
Tirth Upadhyay
920 PointsTirth Upadhyay
920 Pointslike we use : Console.ReadLine();
To get the input from User in form of String.
What is the command for getting input in form of integer?
Plus when we know that the user is surely going to add numbers(Integers) why do we apply string and not integer directly? Though Thank you so much for your reply. :)
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherReadLine is a method on the Console object. And it returns a string. Here's the MSDN documentation.
https://msdn.microsoft.com/en-us/library/system.console.readline(v=vs.110).aspx
We're not really applying a string. It's what is returned by the ReadLine method as noted in the documentation above. This is just the way this method works. There is no command for getting input in the form of an integer. That being said, other languages have similar methods and they also return strings. All that I know about anyways.
But, unfortunately, users are human. Some of them are dumb, and some of them make honest mistakes like a typo. So if you're asking for an integer, but they accidentally type 4e (given that the keys are so close together) then your program is going to crash if it's not actually an integer. Now, later on you'll learn about Exceptions and how to handle them in the case that your user types in the wrong thing, but you're not quite there yet.
Hope this helps!
Tirth Upadhyay
920 PointsTirth Upadhyay
920 PointsThank you so much! That cleared all my doubts! Looks like you have completed almost everything on Treehouse. I just wanted your feedback that after learning c# basics and c#objects will i be able to code scripts for my games in unity???
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherTirth Upadhyay Lol! Oh no no no... I'm far from done But to answer your question, yes, I think you will have the foundations needed to get started. But I myself have just started the game development courses here. So I can't say for sure.
Tirth Upadhyay
920 PointsTirth Upadhyay
920 PointsThank you so much for your feedback! hope u continue answering my questions.