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# C# Basics C# Syntax Types

Why 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

Thank you

Hello 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

        }
    }
}