1

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]);
    }
John Saunders
  • 160,644
  • 26
  • 247
  • 397
dapinder
  • 33
  • 1
  • 1
  • 4
  • 3
    `Int32 decimals = 2` - Are default parameter specifiers allowed in .NET 2.0? I don't think they were introduced until sometime later. – David Jul 10 '13 at 17:54
  • 1
    Did you mean that you want help understanding the problem or do you mean how can you do the equivalent of default parameters when they are not available to you? – Chris Jul 10 '13 at 17:56

3 Answers3

5

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

Bill Gregg
  • 7,067
  • 2
  • 22
  • 39
3

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]);
}
Cemafor
  • 1,633
  • 12
  • 27
1

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

Community
  • 1
  • 1
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185