Read Integer From Console in C#

  1. Read Integer From Console With the int.Parse() Method in C
  2. Read Integer From Console With the int.TryParse() Method in C
  3. Read Integer From Console With the Convert.ToInt32() Method in C

This tutorial will discuss the methods to read an integer value from the console in C#.

Read Integer From Console With the int.Parse() Method in C

By default, the Console.ReadLine() method in C# reads a string value from the console. If we want to read an integer value from the console, we first have to input the integer value in a string and then convert it to an integer. The int.Parse() method is then used to convert a string to an integer value in C#.

using System;

namespace read_integer
{
    class Program
    {
        static void method1()
        {
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Enter a number");
            int num = int.Parse(Console.ReadLine());
            Console.WriteLine("You entered {0}",num);
        }
    }
}

Output:

Enter a number
11
You entered 11

In the above code, we read the integer variable num from the console with the int.Parse() method in C#. We first get the input in the form of a string with the Console.ReadLine() method and then convert it to an integer with the int.Parse() method.

Read Integer From Console With the int.TryParse() Method in C

The int.TryParse() method in C# can also convert a string value to an integer value. The only difference is that the int.TryParse() method has a boolean return type and returns false if there is no integer value inside the string. The int.TryParse() method checks whether the input is a valid integer or not. Our program will not throw an exception if someone inputs any invalid values.

using System;

namespace read_integer
{
    class Program
    {
        static void method2()
        {
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Enter a number");
            if(!int.TryParse(Console.ReadLine(), out int num))
            {
                Console.WriteLine("Invalid value entered");
            }
            else
            {
                Console.WriteLine("You entered {0}", num);
            }
        }
    }
}

Output:

Enter a number
12
You entered 12

In the above code, we read the integer variable num from the console with the int.TryParse() method in C#. We check whether the user entered a valid integer or not with the int.TryParse() method. If the value is invalid, the program displays an error message. If the value is valid, the program stores the value inside the integer variable num and displays it in the output. This method is better than the previous approach because it provides us with a way to handle unexpected user values. The int.TryParse() method should be preferred over the int.TryParse() method when we are uncertain about the user input.

Read Integer From Console With the Convert.ToInt32() Method in C

The Convert class is used to convert between different base data types in C#. Since both string and integer are base data types, we can convert from a string variable to an integer variable with the Convert class. The Convert.ToInt32() method is another way to convert a string value to an integer value in C#. We can read the string from the console with the Console.ReadLine() method and then convert it to an integer value with the Convert.ToInt32() method.

using System;

namespace read_integer
{
    class Program
    {
        static void method3()
        {
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Enter a number");
            int num = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("You entered {0}", num);
        }
    }
}

Output:

Enter a number
44
You entered 44

In the above code, we read the integer variable num from the console with the Convert.ToInt32() method in C#. We first get the input in the form of a string with the Console.ReadLine() method and then convert it to an integer with the Convert.ToInt32() method.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Csharp Console

  • Properly Exit an Application in C#