xFactorSchool.com(Beta)
How to Do Examples
Python Examples
HTML Examples
Code Examples
C Code Examples
C++ Code Examples
C# Code Examples
Java Code Examples
Python Code Examples
HTML Code Examples
CSS Code Examples
JavaScript Code Examples
Online Editor
C Code Editor
C++ Code Editor
C# Code Editor
Java Code Editor
Python Code Editor
HTML Code Editor
CSS Code Editor
JavaScript Code Editor
Practice how to convert string data to integer or decimal types using TryParse() method in C#
C# Online Editor
Execute Code
More C# Sample Code
using System; namespace ExampleApp { class Program { static void Main(string[] args) { // Convert string to integer int outputInteger; string stringVariable = "1225"; // this string data will be coverted as it has T if (int.TryParse(stringVariable, out outputInteger)) { Console.WriteLine(outputInteger + 25); // 25 added with the integer. Output will be 1250 } else { Console.WriteLine(stringVariable + " cannot be converted to integer."); } // Convert string to decimal decimal outputDecimal; string stringNewVariable = "1225.25"; // this string data will be converted to decimal if (decimal.TryParse(stringNewVariable, out outputDecimal)) { Console.WriteLine(outputDecimal + 12.46m); //12.46 added with the decimal. Output will be 1237.71 } else { Console.WriteLine(stringVariable + " cannot be converted to decimal."); } //another example int outputInteger2; string stringVariable2 = "T1225"; // this string data will not be coverted as it has T if (int.TryParse(stringVariable2, out outputInteger2)) { Console.WriteLine(outputInteger2 + 25); } else { Console.WriteLine(stringVariable2 + " cannot be converted to integer."); } } } }