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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:40:00+00:00 2026-05-17T18:40:00+00:00

Dynamic should be able to handle math without making me have to think about

  • 0

Dynamic should be able to handle math without making me have to think about it but even in trivial cases I’m running into some issues.
Consider this really simple function::

 public static T DynamicFactorial<T>(T input)
  {
   dynamic num = input;
   dynamic res = 1; 
   for (; num > 1; res *= num, num -=1) ;
   return res;
  }

This is a function that should handle taking any numeric type and performing a factorial on it. Unfortunately, this gives me the following exception, when I try to compute DynamicFactorial(5UL):

Operator '*=' cannot be applied to operands of type 'int' and 'ulong'

Please don’t say I can turn this code into a recursive call, as this is an example. My problem is that if you are trying to use dynamic to use unary assignment operator, it doesn’t make sense to force me to know my types being computed at compile time. A “potential” solution is to do this::

  public static T DynamicFactorial<T>(T input)
  {
   dynamic num = input;
   T ONE = (T)(1 as dynamic); 
   dynamic res = ONE; 
   for (; num > ONE; res *= num, num -=ONE) ;
   return res;
  }

This works, but holy hell is this ugly and requires me to create a constant of the type I plan on actually using, which is crappy to say the least.

  • 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-17T18:40:01+00:00Added an answer on May 17, 2026 at 6:40 pm

    The fundamental design principle of “dynamic” is that the analysis at runtime is exactly the same as the analysis would have been at compile time if the compiler had been given the runtime types.

    So let’s take a modified version of your code:

     ulong input = whatever;
     dynamic num = input; 
     dynamic res = 1;  
     res = res * num;
    

    That should behave at runtime exactly as though the compiler had known the types of everything marked as “dynamic”. It should behave exactly as

     ulong input = whatever;
     object num = input; 
     object res = 1;  
     res = (int)res * (ulong)num;
    

    And that program gives an error at compile time, so logically the dynamic version must give the same error at runtime.

    Dynamic should be able to handle math without making me have to think about it

    Absolutely not. That is not a design principle of the dynamic feature. The purpose of the dynamic feature is to simplify interaction of C# code with code in libraries designed to interact with dynamic languages, either modern libraries (like those designed for Python and Ruby), or legacy libraries (like those designed for COM automation via VB6 or VBScript). Doing VB-style type promotion on results of arithmetic expressions was not at all on our minds when we designed this feature, and as you’ve discovered, it does so badly.

    Let me make this perfectly clear: dynamic is not about making C# a dynamic language, which seems to be what you think it is about. Dynamic is about making C# a statically typed language that interoperates well with libraries designed for dynamic languages. If what you want is a language with dynamic arithmetic, consider Visual Basic or Python.

    (Incidentally, some might wonder why int + ulong is not legal in C#. There are seven nonlifted numeric addition operators in C#: int+int, uint+uint, long+long, ulong+ulong, float+float, double+double and decimal+decimal. Of these seven, which one is the best? int+int, uint+uint and long+long are out because the ulong might not fit. ulong+ulong is out because the int might be negative. That leaves float, double and decimal. Float is better than double (because it is more specific) so double goes away. But converting a ulong to float is neither better nor worse than converting a ulong to decimal. Since we have an ambiguity here we produce an error. Insert a cast to resolve the ambiguity if for some bizarre reason you have to add an int to a ulong.)

    Finally, I note that there are ways to do what you want. I haven’t actually tried it but this would probably work:

    public static T DynamicFactorial<T>(T input) 
    { 
        dynamic num = input; 
        dynamic one = default(T);
        one++;
        dynamic res = one;  
        while (num > one)
        {
            res *= num;
            num--;
        }
        return res; 
    } 
    

    This will work for any type where the default is zero and there are ++, –, and * operators defined on it.

    However, this is gross, slow, and an abuse of generics. Are you really going to want to compute factorials of ushort? Factorial is an easy function to define and you’re probably not going to need more than half a dozen versions of it, tops. I say just write it half a dozen times rather than saving a tiny number of keystrokes by abusing generics and dynamic.

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

Sidebar

Related Questions

Suppose I have an ELF binary that's dynamic linked, and I want to override/redirect
We have a requirement, in which we need to create dynamic word document and
Background I am designing a system which enables the development of dynamic authentication schemes
There seems to be a problem between how PHP engine handles identical files that
I'm looking for existing middleware solutions that address aspects of service clustering/distribution for load-balancing
I need to create two controls that contain the same amound of items (a
I guess the title doesn't really say a lot, so here's the explanation. I
I'm working on a project that's a .NET extension to a rather large classic
I'm creating a turn and text-based strategy game in Django on Google App Engine

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.