Skip to main content

 Here is a code snippet for checking if string is number in C#

/// <summary>
/// Method checks if passed string is integer
/// </summary>
/// <param name="text">string text for checking</param>
/// <returns>bool - if text is integer return true, else return false</returns>
public bool IsNumber(string text)
{
    int num = 0;
    bool isNum = false;
  
    // Check for empty string.
    if (string.IsNullOrEmpty(text))
    {
        return false;
    }
  
    isNum = int.TryParse(text, out num);
  
    return isNum;
}

- Advertisement -
- Advertisement -