Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7746389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:15:59+00:00 2026-06-01T10:15:59+00:00

I find myself often needing to use int.TryParse() to test if a value is

  • 0

I find myself often needing to use int.TryParse() to test if a value is an integer. However, when using TryParse, I have to pass a reference variable to the function. So I find myself always needing to create a temp int to be passed in. Usually it looks something like:

int my_temp_integer;

int.TryParse(potential_integer, my_temp_integer);

I find this to be quite cumbersome considering that all I want is a simple true/false response, and I don’t care about the actual parsed result. Is there a better way to approach this? Why isn’t there an overloaded function where I can just pass the value I want to test and get a true/false response?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T10:16:01+00:00Added an answer on June 1, 2026 at 10:16 am

    you could write an extension method:

    public static bool IsInt(this string pString)
    {
       int value;
       return int.TryParse(pString, out value);
    }
    

    then your example becomes:

    potential_integer.IsInt();
    

    EDIT:
    Lately I have been using a generic form of this.

    public delegate bool TryParser<T>(string pString, out T pResult);
    
    public static bool Is<T>(this string pString, TryParser<T> pTryParser)
    {
       T val;
       return pTryParser(pString, out val);
    }
    

    Can then use it as follows; it’s not perfect, but it’s more concise than anything I’ve found:

    "1234".Is<int>(int.TryParse);       // true
    "asdf123".Is<int>(int.TryParse);    // false
    "1.2345".Is<float>(float.TryParse); // true
    "1000".Is<byte>(byte.TryParse);     // false
    

    Theoretically, this would also work with custom TryParse methods, as long as you followed the same pattern as the official ones.

    Update: If you maintain a static dictionary of TryParse methods by type, you can avoid having to ever directly pass the method. The dictionary can even be populated as needed with reflection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I find myself often needing to use Integer.TryParse to test if a value is
I often find myself needing reference to an object that is several objects away,
I find myself often needing addClassName and removeClassName functions for Element s. However, I
I find myself often faced with this problem: I have a dictionary where the
I often find myself wanting to just print (using the implicit toString() of each
I often find myself using lambdas as some sort of local functions to make
I often find myself needing a quick ( in terms of code ), lightweight
Many try/except/finally-clauses not only uglify my code, but i find myself often using identical
I often find myself using Integers to represent values in different spaces. For example...
I often find myself needing to write functions to load/save from/to ASCII (or similar)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.