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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:46:58+00:00 2026-06-06T17:46:58+00:00

I have a method: public void StoreNumberInSmallestType(ValueType number) { if (numberTypes == null) numberTypes

  • 0

I have a method:

public void StoreNumberInSmallestType(ValueType number)
{
    if (numberTypes == null)
        numberTypes = new List<Type>() { typeof(sbyte), typeof(short), typeof(int), typeof(long), typeof(float), typeof(double) };

    foreach (Type t in numberTypes)
    {
        try
        {
            var converter = TypeDescriptor.GetConverter(t);
            value = converter.ConvertTo(number, t);

            Type = value.GetType();

            return;
        }

        catch (OverflowException) { }
    }
}

The method is inside a class where the variable value is defined as dynamic.

When used like this:

StoreNumberInSmallestType(Math.Pow(200, 100));

value ends up being Infinity. If I step through the process, I find that the value of number is not Infinity, but is the result expressed in scientific notation. Something bad is happening whenever number gets converted and stored inside value. Does anybody know why number holds the correct value, but value does not?

EDIT:

Here is a complete code sample:

Main:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Class1 c1 = new Class1();
            c1.StoreNumberInSmallestType(Math.Pow(200, 100));
        }
    }
}

Class:

namespace ConsoleApplication1
{
    public class Class1
    {
        List<Type> numberTypes;
        dynamic value;
        public Type Type { get; set; }

        public void StoreNumberInSmallestType(ValueType number)
        {
            if (numberTypes == null)
                numberTypes = new List<Type>() { typeof(sbyte), typeof(short), typeof(int), typeof(long), typeof(float), typeof(double) };

            foreach (Type t in numberTypes)
            {
                try
                {
                    var converter = TypeDescriptor.GetConverter(t);
                    value = converter.ConvertTo(number, t);

                    Type = value.GetType();

                    return;
                }

                catch (OverflowException) { }
            }
        }
    }
}
  • 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-06T17:46:59+00:00Added an answer on June 6, 2026 at 5:46 pm

    You don’t get infinity for the double conversion, but for the float conversion.
    There you get Infinity and no exception and then your code returns before you get to the double conversion.

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

Sidebar

Related Questions

I have the following method public static void SerializeToXMLFile(Object obj,Type type, string fileName) {
I have new problem. My code: .method public static void Main() cil managed {
I have a method: public void StoreUsingKey<T>(T value) where T : class, new() {
I have a SomeClass implements InterfaceA . I have a method: public void doSomething(List<SomeClass)
I have the below method: public void Enqueue(ICommand itemToQueue) { if (itemToQueue == null)
I have a method: public static void GetObjects() { using(MyContext context = new MyContext())
I have a method public static void startAnimation() { new AnimationThread().run(); } where AnimationThread
I have this method: public void testJSNI2(){ String x = test; } I can
I have the following method: public void PutFile(string ID, Stream content) { try {
If I have a method like this: public void DoSomething(int Count, string[] Lines) {

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.