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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:16:29+00:00 2026-05-22T19:16:29+00:00

To parse a string to an int, one calls Int32.Parse(string) , for double, Double.Parse(string)

  • 0

To parse a string to an int, one calls Int32.Parse(string), for double, Double.Parse(string), for long, Int64.Parse(string), and so on..

Is it possible to create a method that makes it generic, for example, ParseString<T>(string)? where T can be Int32, Double, etc. I notice the number of types don’t implement any common interface, and the Parse methods don’t have any common parent.

Is there any way to achieve this or something similar to this?

  • 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-22T19:16:29+00:00Added an answer on May 22, 2026 at 7:16 pm

    You’d basically have to use reflection to find the relevant static Parse method, invoke it, and cast the return value back to T. Alternatively, you could use Convert.ChangeType or get the relevant TypeDescriptor and associated TypeConverter.

    A more limited but efficient (and simple, in some ways) approach would be to keep a dictionary from type to parsing delegate – cast the delegate to a Func<string, T> and invoke it. That would allow you to use different methods for different types, but you’d need to know the types you needed to convert to up-front.

    Whatever you do, you won’t be able to specify a generic constraint which would make it safe at compile-time though. Really you need something like my idea of static interfaces for that kind of thing. EDIT: As mentioned, there’s the IConvertible interface, but that doesn’t necessarily mean that you’ll be able to convert from string. Another type could implement IConvertible without having any way of converting to that type from a string.

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

Sidebar

Related Questions

I want to parse a string that represent a DateTime in UTC format. My
I have to parse a String to create a PathSegmentCollection . The string is
I'm trying to parse a string that was generated by an NSDateFormatter using another
I have a static method like this public static string DoSomethingToString(string UntrustedString) { //parse
I need to parse string from datapicker to a DataTime object and save in
I'm using java.text.SimpleDateFormat to parse string representations of date/time values inside an XML document.
How to parse the string {'result':(Boolean, MessageString)} using Python regular expressions to get Boolean
How can I parse a string like 01-Jan-1995 to a Python datetime object?
Im trying to parse the string located in /proc/stat in a linux filesystem using
I'm trying to parse a string in a specific format and I'm really surprised

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.