Click here to Skip to main content
15,887,965 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
//I have this method that use dynamic
public static dynamic Fact(dynamic r)
{
    dynamic result = 1;
    for (dynamic i = 1; i <= r; i++)
    {
        result *= i;
    }
    return result;
}
//I call it in main as
static void Main(string[] args)
{
    for (int i = 9; i < 100; i++)
    {
        dynamic x=helper_method.Fact(i);
        Console.WriteLine("{0}\t>>{1}\t{2}", i, x,x.GetType());
    }
}
//the output type is Int32 at all
//I think since it is dynamic the type will changed when match maximum int32
//and dynamic will automatically change type Int32 to Int64

//what is the benefit of dynamic if it's type not changed at runtime?????????

//output here
/*
9       >>362880        System.Int32
10      >>3628800       System.Int32
11      >>39916800      System.Int32
12      >>479001600     System.Int32
13      >>1932053504    System.Int32
14      >>1278945280    System.Int32
15      >>2004310016    System.Int32
16      >>2004189184    System.Int32
17      >>-288522240    System.Int32
18      >>-898433024    System.Int32
19      >>109641728     System.Int32
20      >>-2102132736   System.Int32
21      >>-1195114496   System.Int32
22      >>-522715136    System.Int32
23      >>862453760     System.Int32
24      >>-775946240    System.Int32
25      >>2076180480    System.Int32
26      >>-1853882368   System.Int32
27      >>1484783616    System.Int32
28      >>-1375731712   System.Int32
29      >>-1241513984   System.Int32
30      >>1409286144    System.Int32
31      >>738197504     System.Int32
32      >>-2147483648   System.Int32
33      >>-2147483648   System.Int32
34      >>0     System.Int32
35      >>0     System.Int32
36      >>0     System.Int32
37      >>0     System.Int32
38      >>0     System.Int32
39      >>0     System.Int32
40      >>0     System.Int32
41      >>0     System.Int32
42      >>0     System.Int32
43      >>0     System.Int32
44      >>0     System.Int32
45      >>0     System.Int32
46      >>0     System.Int32
*/
Posted

HEres a pretty good explination of what the dynamic really is:
http://msdn.microsoft.com/en-us/magazine/ee336309.aspx[^]
 
Share this answer
 
Comments
ibrahim_ragab 4-Apr-13 17:12pm    
Thank you very much
It is very good article
ibrahim_ragab 4-Apr-13 17:16pm    
In article
"Type of a variable has to be statically determined at compile time and can never change while the variable is in scope"
Sergey Alexandrovich Kryukov 4-Apr-13 17:53pm    
5ed.
—SA
Yes it is NOT the dynamic you think it is (should it be a numeric range-extender?).
Check out the documentation[^] to understand its purpose and the usage scenarios.
 
Share this answer
 
Comments
ibrahim_ragab 4-Apr-13 16:58pm    
Thank you,
"The dynamic is a static type."
Microsoft is so funny.
CPallini 5-Apr-13 1:40am    
Might be. However they provide documentation. :-)
Sergey Alexandrovich Kryukov 4-Apr-13 17:55pm    
5ed. Fantasy is one of the most important things, but in most people, it often misses the target. :-)
—SA
CPallini 5-Apr-13 1:39am    
Thank you.
Dynamic is just not for that.
You are right it is not what you thought it is.
The dynamic object can hold much anything, but won't change. At the point you set result=1 it becomes int32. And the multiplication expression result type won't be set based on it's result, but based on the operands, that will be int32 in your case.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900