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 trialRodrigo Muñoz
Courses Plus Student 20,171 PointsWhy if I store Console.ReadLine() as a variable always become a string type even if I enter a number?
Here's is the code I tried
using System;
namespace learning
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, What's your name? ");
var name = Console.ReadLine();
Console.WriteLine(name.GetType());
}
}
}
2 Answers
Reggie Williams
Treehouse TeacherHey Rodrigo Muñoz ! The ReadLine method always returns a string
rg10
1,069 PointsHello Rodrigo Muñoz , you might force it to be stored as int but an exception will occur if the value is not a number:
using System;
namespace StringTrain
{
class Program
{
static void Main(string[] args)
{
int input = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(input.GetType());
//Output: System.Int32
}
}
}
Rodrigo Muñoz
Courses Plus Student 20,171 PointsRodrigo Muñoz
Courses Plus Student 20,171 PointsThank you