如何在没有多次转换的情况下逐字定义小数组?

Edw*_*uay 13 c# arrays casting decimal

如何定义一个小数组而不显式地每个小数组?

//decimal[] prices = { 39.99, 29.99, 29.99, 19.99, 49.99 }; //can't convert double to decimal
//var prices = { 39.99, 29.99, 29.99, 19.99, 49.99 }; //can't initialize... 
decimal[] prices = { (decimal)39.99, (decimal)29.99, (decimal)29.99, (decimal)19.99, (decimal)49.99 };
Run Code Online (Sandbox Code Playgroud)

Asa*_*aph 29

使用m后缀.

decimal[] prices = { 39.99m, 29.99m, 19.99m, 49.99m };
Run Code Online (Sandbox Code Playgroud)

如果没有m(或M)后缀,编译器会将其视为double.

- http://msdn.microsoft.com/en-us/library/364x0z75(VS.71).aspx


Ken*_*ick 10

你需要在最后添加一个M.