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 7413121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:38:51+00:00 2026-05-29T06:38:51+00:00

I am developing a generic wrapper around TryParse, as follows: public delegate bool ParseDelegate<T>(string

  • 0

I am developing a generic wrapper around TryParse, as follows:

    public delegate bool ParseDelegate<T>(string s, out T result);

    public static T? ParseOrNull<T>(this string value, ParseDelegate<T> parse) where T : struct
    {
        T result;
        var parsed = parse(value, out result);
        return parsed ? result : (T?)null;
    }

    [Test]
    public void ParsesValidInt()
    {
        Assert.AreEqual(1234, "1234".ParseOrNull<int>(int.TryParse));
    }

    [Test]
    public void ParsesValidDecimal()
    {
        Assert.AreEqual(12.34M, "12.34".ParseOrNull<decimal>(decimal.TryParse));
    }

This is kinda repetitive. Is there a way to avoid mentioning int.TryParse at all, so that my calls look as follows:

"1234".ParseOrNull<int>()
  • 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-05-29T06:38:52+00:00Added an answer on May 29, 2026 at 6:38 am

    Is there a way to avoid mentioning int.TryParse at all, so that my calls look as follows:

    Not directly, as TryParse isn’t part of a shared interface. If there were a shared interface to these value types, this would be possible via a constraint.


    Personally, I would not suggest using extension methods for this. I would rather write this as something more like:

    public static class Parse
    {
        public delegate bool ParseDelegate<T>(string s, out T result);
        public static T? FromString<T>(string value, ParseDelegate<T> parse) where T : struct
        {
            T result;
            var parsed = parse(value, out result);
            return parsed ? result : (T?)null;
        }
        public static int? ToNullableInt32(string value)
        {
            return FromString<int>(value, int.TryParse);
        }
        public static double? ToNullableDouble(string value)
        {
            return FromString<double>(value, double.TryParse);
        }
    }
    

    This adds a bit of overhead up front, but allows you to write these very cleanly, ie:

        int? first = Parse.FromString<int>("1234", int.TryParse);
        int? second = Parse.ToNullableInt32("1234");
        double? third = Parse.ToNullableDouble("1234");
    

    I see little value in putting an extension method, especially on something like string (which is used everywhere), as it “pollutes” the compilation of string itself. You’ll see this everywhere you use strings – basically, any time you use this namespace, you’ll end up having these parse methods in your intellisense, etc. In addition, this seems more like a “utility” than something that should appear as built-in functionality of string itself, which is why I personally prefer a separate class for it.

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

Sidebar

Related Questions

I am developing a generic layer that allows me to perform the basic save,
I have a C program that I'm developing using Ubuntu 11.10 (Linux version 3.0.0-12-generic-pae
Developing an interface generic I wished to declare a constructor in an interface but
I was developing a generic linked list. Although the compiler doesn't give any error
I'm developing a web app that emulates a generic file browser for the desktop,
I am developing a generic logging object which will be used within all of
i'm developing a authentication backend with object-based permissions for my django-app.I use generic relations
Developing websites are time-consuming. To improve productivity, I would code a prototype to show
Developing a .NET WinForms application: how can I check if the window is in
Developing a heavily XML-based Java-application, I recently encountered an interesting problem on Ubuntu Linux.

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.