Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello ......

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



I got this "Default parameter specifiers are not permitted" error on "Int32 decimals = 2"

in vs2008 and .net framework 2.o
Posted
Updated 13-Apr-23 11:01am
v2

1 solution

You cannot specify optional parameters in C# prior to C# 4.0.
http://msdn.microsoft.com/en-gb/library/dd264739.aspx[^]

Instead, you can provide an overloaded method which accepts the other parameters and calls the other method using the required default. Thus:
C#
private string formatSizeBinary(Int64 size, Int32 decimals)
{
   // ... your code here ...
}

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

Regards,
Ian.
 
Share this answer
 
v2
Comments
phil.o 26-Mar-13 5:23am    
5'd.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900