0
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"

4

1 に答える 1

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);
于 2013-03-26T08:42:27.767 に答える