Search code examples
c#integerconverters

How does Convert.ToInt32(Console.ReadLine()); work?


I am new to programming in C# and i'm currently doing an exercise that looks like this:

  1. Read the user's first and last name and save this in variables
  2. Welcome the user
  3. Ask the user about their age this year and save this in a variable with the appropriate data type
  4. Calculate previous days the person has lived based on the specified age (age multiplied by 365) and present this.

So my code is working fine, but i would really appreciate if someone could explain the convert thing. I've been making alot of google searches and reading, but i can't seem to understand how it works.

int age = Convert.ToInt32(Console.ReadLine());

Can someone break this down for me for my code?

Thanks!

        Console.Title = "Programming in C # - Exercise 1";

        // Here the system asks the user to enter their full name.
        Console.WriteLine("System: Enter your full name: ");

        // Here I declare a variable and assign it a value.
        string name = Console.ReadLine();

        // Here the system welcomes the user.
        Console.WriteLine("System: Welcome " + name + "!");

        // Here the system asks how old the user is.
        Console.WriteLine("System: How old are you?");

        // This is the part i would like to have explained for me.
        int age = Convert.ToInt32(Console.ReadLine());

        // Here I declare a variable and calculate age in days.
        int ageInDays = age * 365;

        // Here the system answers.
        Console.WriteLine("System: " + age + "?!" + " That means you are " + ageInDays + " days old!");

        // Waiting to close the program.
        Console.ReadLine();

Solution

  • Although you have a understanding now I'd like to point out that developers/coders should always expect the unexpected e.g. a user enters the wrong type such as for age a value that can not be converted to a integer or the case of name, they simply press enter without entering a name.

    So for checking in this case for name being entered use string.IsNullOrWhiteSpace. For integer type, consider using int.TryParse.

    In regards to concatenating string values or string values say with integers consider looking at string interpolation, this can be coder preference as some will like to string someString + anotherString or if using strings only there is String.Concat and StringBuilder Class.

    Below is a slightly modified version of your code. Since you are just starting out, tuck this away for later or consider learning from this now than later.

    using System;
    
    namespace Exercise1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.Title = "Programming in C # - Exercise 1";
    
                Console.WriteLine("System: Enter your full name: ");
    
                string fullName = Console.ReadLine();
                
                /*
                 * Never assume a value for name has been entered
                 * https://learn.microsoft.com/en-us/dotnet/api/system.string.isnullorwhitespace?view=net-5.0
                 */
                if (!string.IsNullOrWhiteSpace(fullName))
                {
                    /*
                     * Note original code concatenated name variable with text, you can also use string interpolation
                     * https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated
                     */
                    Console.WriteLine($"System: Welcome {fullName}!");
                }
                else
                {
                    Console.WriteLine("System: Welcome no name given!");
                }
    
    
                Console.WriteLine("System: How old are you?");
                string ageInput = Console.ReadLine();
    
                /*
                 * int.TryParse, better than Convert.ToInt32 which is recommended in the remark section
                 * for documentation for Convert.ToInt32.
                 *
                 * https://learn.microsoft.com/en-us/dotnet/api/system.int32.tryparse?view=net-5.0
                 *
                 */
                if (int.TryParse(ageInput, out var age))
                {
                    int ageInDays = age * 365;
                    /*
                     * Note original code concatenated name variable with text, you can also use string interpolation
                     * https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated
                     */
                    Console.WriteLine($"System: {age}?! That means you are {ageInDays} days old!");
                }
                else
                {
                    Console.WriteLine("No age provided or the value was not a valid integer");
                }
    
                Console.WriteLine("Press any key to close this program");
                Console.ReadLine();
    
            }
        }
    }