【问题标题】:Default parameter specifiers are not permitted in c# desktop applicationc# 桌面应用程序中不允许使用默认参数说明符
【发布时间】:2013-03-26 08:35:27
【问题描述】:
private string formatSizeBinary(Int64 size, Int32 decimals = 2)
        {
            string[] sizes = { "Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
            double formattedSize = size;
            Int32 sizeIndex = 0;
            while (formattedSize >= 1024 & sizeIndex < sizes.Length)
            {
                formattedSize /= 1024;
                sizeIndex += 1;
            }
            return string.Format("{0} {1}", Math.Round(formattedSize, decimals).ToString(), sizes[sizeIndex]);
        }

我收到了

“不允许使用默认参数说明符”

"Int32 decimals = 2" 出错

【问题讨论】:

  • C# 中的可选参数仅在 C# 2010(和 VS2010)中引入。如果(如您的标签所示)您使用的是 .NET 2.0 和 VS2008,那么错误消息几乎是不言自明的。你有什么问题?

标签: c# visual-studio-2008 .net-2.0


【解决方案1】:

因为您的代码对我来说看起来不错,但 Optional parameters 带有 Visual Studio 2010(可能还有 .NET 4.0 框架)

Visual C# 2010 引入了命名参数和可选参数

你需要一个类似的方法;

private string formatSizeBinary(Int64 size, Int32 decimals, int value)
        {
            decimals = value;
            string[] sizes = { "Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
            double formattedSize = size;
            Int32 sizeIndex = 0;
            while (formattedSize >= 1024 & sizeIndex < sizes.Length)
            {
                formattedSize /= 1024;
                sizeIndex += 1;
            }
            return string.Format("{0} {1}", Math.Round(formattedSize, decimals).ToString(), sizes[sizeIndex]);
        }

然后你就可以调用你想要的值了;

formatSizeBinary(yoursize, decimals, 2);
formatSizeBinary(yoursize, decimals, 3);
formatSizeBinary(yoursize, decimals, 4);

【讨论】:

  • C# 不支持默认参数这是错误
  • 如果我想传递小数=3 而不是我应该怎么做?
  • 它是写的,但我的问题是传递其他值而不是我应该做什么?
猜你喜欢
  • 2011-12-10
  • 2013-12-27
  • 1970-01-01
  • 2011-09-17
  • 1970-01-01
  • 2014-09-28
  • 1970-01-01
  • 2014-12-10
  • 2015-02-20
相关资源
最近更新 更多