C# int.TryParse Method


Convert a string representation of number to an integer, using the int.TryParse() method in C#. If the string cannot be converted, then the int.TryParse() method returns false i.e. a Boolean value.

Let’s say you have a string representation of a number.

string myStr = "12";

Now to convert it to an integer, use the int.TryParse(). It will get converted and will return True.

int.TryParse(myStr, out a);

Example

 Live Demo

using System.IO;
using System;
class Program {
   static void Main() {
      bool res;
      int a;
      string myStr = "12";
      res = int.TryParse(myStr, out a);
      Console.WriteLine("String is a numeric representation: "+res);
   }
}

Output

String is a numeric representation: True

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 02-Sep-2023

49K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements