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

  • Home
  • SEARCH
  • 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 6038037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:09:37+00:00 2026-05-23T06:09:37+00:00

I need a generic method that can return a value parsed from a string

  • 0

I need a generic method that can return a value parsed from a string

public T GetDefaultValue<T>(){
    // if typeof(T) is Double it should try to parse some string (supposedly which's been read from DB), 
    // and return Double value, or if typeof(T) is Int, then it should parse the string 
    //into Int, and finally if typeof(T) is a string, then no parsing is needed.
}

UPD… Why can’t I check if T is some certain type and use Parse method accordingly?

  • 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-23T06:09:38+00:00Added an answer on May 23, 2026 at 6:09 am

    Convert.ChangeType could be used to make your conversions.

    public T Parse<T>(string input)
    {
         return (T)Convert.ChangeType(input, typeof(T));
    }
    
    int x = Parse<int>("1");
    double y = Parse<double>("1.0");
    string z = Parse<string>("hey");
    

    Notice that to even make this work, you are specifying your types? What’s the savings over using int.Parse (or TryParse), double.Parse, etc.?

    And since you mention your inputs are likely coming from a database, and still knowing that to even use the above method you have to specify the type parameter already, I would encourage you to (a) know and trust your types and (b) use existing functionality to obtain values from data sources. No need to convert something to a string and then convert it back to whatever data type you wish it to be.

    int x = myDataRow.Field<int>("Column1"); // or
    int x = (int)myDataRow["Column1"];
    

    This also supports if the numeric values could be nullable in the database.

    int? x = myDataRow.Field<int?>("Column1"); // or
    int x = myDataRow.Field<int?>("Column1").GetValueOrDefault(); // normalize nulls to 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a generic container that keeps its elements sorted and can be asked
I need to write a generic procedure that set value for one column or
I'm trying to return a strongly typed Enumeration value from a string. I'm sure
I have a method (shown below) that I discovered can be reused in code
Imagine C# code like this ISomeInterface someVar = creator.Create(typeof(SomeClass), typeof(ISomeInterface)); What should happen? Method
I want to make a generic method that will take either a single object
I'm wanting to write a method that I can use to initialise a Map.
FilenameFilter has an accept method that I can implement to tell system how I
I need a way to create an extension method off of an IEnumerable that
I'm creating a generic class and in one of the methods I need to

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.