1

我在我的程序的以下代码块中收到此错误。我正在将 c# 与 .net 2.0 一起使用。它在代码的第一行中以小数 = 2显示此错误。请帮忙

 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]);
    }
4

3 回答 3

5

默认参数在 .Net 2 中不可用。

它们仅在 .Net 4.0 中可用:

http://msdn.microsoft.com/en-us/library/dd264739.aspx

于 2013-07-10T17:54:51.417 回答
3

如果需要此功能,则必须恢复为重载方法,因为在 C# 2.0 中“不允许使用默认参数说明符”。

private string formatSizeBinary(Int64 size)
{
    return formatSizeBinary(size, 2);
}

private string formatSizeBinary(Int64 size, Int32 decimals)
{
    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]);
}
于 2013-07-10T17:58:07.347 回答
1

引入了默认参数C# 4.0以更具体。但是.Net framework 2.0,只要您在VS2010. 从这里的答案-

自 1.0 起,CLR 已支持默认参数。像 VB.Net 这样的语言从一开始就一直在使用它们。虽然支持它们的第一个 C# 版本是 4.0,但它仍然可以为 2.0 CLR 生成有效代码,而且事实上确实如此。因此,如果您的目标是 3.5 CLR(或 2.0、3.0 等...),您可以在 2010 年使用默认参数

这种类型的支持不限于默认参数。许多新的 C# 功能可以在旧版本的框架上使用,因为它们不依赖于 CLR 更改。以下是 CLR 2.0 及更高版本支持的更多内容

命名参数:添加了 C# 4.0

Lambda 表达式:添加了 C# 3.0

自动属性:添加了 C# 3.0

扩展方法:添加了 C# 3.0

Co/ContraVariance:添加了 C# 4.0

于 2013-07-10T18:42:21.570 回答