C#动态关键字不是动态的,我觉得!!!!!! [英] C# dynamic keyword not dynamic as i think !!!!!!
本文介绍了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
*/
推荐答案
对动态的真实情况进行了很好的探索:
< a href =http://msdn.microsoft.com/en-us/magazine/ee336309.aspx> http://msdn.microsoft.com/en-us/magazine/ee336309.aspx [< a href =http://msdn.microsoft.com/en-us/magazine/ee336309.aspxtarget =_ blanktitle =新窗口> ^ ]
HEres a pretty good explination of what the dynamic really is:
http://msdn.microsoft.com/en-us/magazine/ee336309.aspx[^]
是的,它不是您认为的动态(它应该是一个数值范围扩展器吗?)。
查看文档 [ ^ ]了解它的用途和使用场景。
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.
动态不是为了那个。
你是对的不是你认为它是什么。
动态对象可以容纳很多东西,但不会改变。在您设置result = 1
时,它变为int32。并且乘法表达式结果类型不会根据它的结果设置,但是基于操作数,在你的情况下将是int32。
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 setresult=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.
这篇关于C#动态关键字不是动态的,我觉得!!!!!!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文