Skip to content

How to take input simple integer in C#?

Syed Arbab Ahmed

Asked Jul 12, 2009, 8:15 AM EDT1 answer
Share
LinkedInX

Hi!

I am a novice in C#, I was trying hard to take simple integer as input in C# but I was having problems, so please could anybody write me a simple program through which I can take simple integer as input.

Thank u.

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Hi,

    Below is a small program which accepts an input by the user. The input value is then checked to verify whether it is an integer or not, and the result is displayed to the user.

    using System;

    class Program
    {
        static void Main(string[] args)
        {
            string input = string.Empty;
            int intValue = 0;

            Console.WriteLine("Insert an integer value");
            input = Console.ReadLine();

            if (int.TryParse(input, out intValue))
                Console.WriteLine(string.Format("Integer value {0} input by user.", intValue));
            else
                Console.WriteLine("User did not enter an integer!");

            Console.ReadLine();
        }
    }

    Hope this helps.

    Cheers
    David
    0