Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to validate a string for a numeric representation using TryParse in C#
The following is our string −
string myStr = "5";
To check whether the above is a string with numeric representation, use TryParse and out.
int.TryParse(myStr, out a);
Here is the complete code.
Example
using System.IO;
using System;
class Program {
static void Main() {
bool res;
int a;
string myStr = "5";
// checking for valid string representation of a number
res = int.TryParse(myStr, out a);
Console.WriteLine(res);
}
}
Output
True
Advertisements