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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:33:12+00:00 2026-05-27T01:33:12+00:00

As an example, I have an several extension methods for converting strings and objects

  • 0

As an example, I have an several extension methods for converting strings and objects into decimals and int32

    public static decimal ToDecimal(this object o)
    {
         return Convert.ToString(o).ToDecimal();
    }

    public static decimal ToDecimal(this string s)
    {
         decimal d = decimal.TryParse(s, out d) ? d : 0;
         return d;
    }

    public static Int32 ToInt32(this object o) { //etc }

But I want to prevent chaining like such:

myobject.ToDecimal().ToDecimal();

or even

myobject.ToDecimal().ToInt32();

If the programmer wants to convert the decimal to int32, then I want him to use Convert.ToInt32() which may just be more efficient since the Convert.ToString part is omitted, and also has more features.

How can I tell C# that only object can use the extension method and not decimal, int etc?

  • 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-27T01:33:13+00:00Added an answer on May 27, 2026 at 1:33 am

    You can’t. This isn’t an extension method issue so much as a general method parameter issue – it will always be valid when the argument can be converted to the method parameter type.

    I can think of a few grotty hack-arounds:

    public static decimal ToDecimal<T>(this T value) where T : class, new()
    

    That would prevent you calling ToDecimal().ToDecimal() as decimal isn’t a class; it would also prevent you calling ToDecimal().ToString().ToDecimal() as string doesn’t have a parameterless constructor.

    I would suggest that it’s not really an appropriate extension method though, to be honest. I would suggest taking more care over how your conversions are performed to start with, and only doing them in very appropriate situations. For example, your current extension method would allow you to convert between double and decimal inappropriately, which isn’t a good idea. Under what circumstances do you really want to do this? Which types are you trying to convert?

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

Sidebar

Related Questions

I'm find I have several places that having public static inner classes designed that
I have several sites in different domains: example.com , example.org , mail.example.com and passport.example.org
There have been several questions posted to SO about floating-point representation. For example, the
Several web service APIs have you sign up for an API key. For example,
I have a table, for example, mytable. It has several columns: col1, col2, col3.
I have a div <div id=masterdiv> which has several child <div> s. Example: <div
Here's the problem. I ,for example,have a string 2500.Its converted from byte array into
I have looked at several variations on the Reachability example such as the Donoho
So I have tried to follow the alfresco example several times and cant seem
I have seen several modules (example: Iterator::Simple ) that make use of Perl's angle

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.