Using TryParse to parse string to integer in C#

You can use the int.TryParse to parse a string in to numbers . The function returns true if it suceeds in parsing the string to integer and false if it doesn’t.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    static class Program
    {
        static void Main()
        {
            data();           
        }
        static void data()
        {
            //
            // Parsing with TryParse
            //
            string txtData = "87";
            int res;
            bool result = int.TryParse(txtData, out res);
            if (result == false)
            {
                MessageBox.Show("Invalid Number");
            }

        }
    }  
}
%d bloggers like this: