Question

I am getting this error in following block of code of my program. I am using c# with .net 2.0. It is showing this error in decimals =2 in first line of the code. Please help

 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]);
    }
Was it helpful?

Solution 2

You will have to revert to overloading the method if you want this functionality because in C# 2.0 "default parameter specifiers are not permitted".

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

OTHER TIPS

Default Parameters were not available in .Net 2.

They only became available in .Net 4.0:

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

Default parameters got introduced in C# 4.0 to be more specific. But you can use them in .Net framework 2.0 as well as long as you are building your solution in VS2010. From the answer here -

Default parameters have been supported in the CLR since 1.0. Languages like VB.Net's have been using them since the start. While the first version of C# to support them is 4.0, it can still generate valid code for a 2.0 CLR and in fact does so. Hence you can use default parameters in 2010 if you are targeting the 3.5 CLR (or 2.0, 3.0, etc ...)

This type of support is not limited to default parameters. Many new C# features can be used on older version of the framework because they do not rely on CLR changes. Here are a few more which are supported on CLR versions 2.0 and above

Named Arguments: Added C# 4.0

Lambda Expressions: Added C# 3.0

Auto Properties: Added C# 3.0

Extension Methods: Added C# 3.0

Co/ContraVariance: Added C# 4.0

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top