Is the new feature of C# 4.0 - "Optional Parameters" CLS-Compliant?
Asked Answered
S

3

31

This new feature is really convenient.

Lately I read the document of the "Microsoft All-In-One Code Framework", and it mentions that "Optional Parameters" is not CLS-Compliant.

So I tested it by using "Optional Parameters" in a public API, and turned on FxCop, then I compiled and FxCop did not complain about anything. At the mean while, FxCop did report a warning when I add an API that has uint as its return type.

So now I am confused, is "Optional Parameters" CLS-Compliant or not?

And what's the best way to find out whether a new language feature is CLS-Compliant or not?

Scruff answered 28/3, 2011 at 9:25 Comment(2)
I believe the proper test would be to set CLSCompliantAttribute and check whether it will compile or not.Dieback
oh, yes. i tried that and i forgot to mention, sorry. it compiled.Lepp
A
28

Optional arguments are "sort-of" CLS-compliant. Methods with optional arguments are legal and can be successfully compiled with the CLSCompliant attribute, but callers of those methods don't necessarily need to take account of the default parameter values or the optional attribute. (In which case those methods would behave in exactly the same way as standard methods, requiring that all the arguments be stated explicitly at the call site.)

Methods that use default parameters are allowed under the Common Language Specification (CLS); however, the CLS allows compilers to ignore the values that are assigned to these parameters. Code that is written for compilers that ignore default parameter values must explicitly provide arguments for each default parameter. To maintain the behavior that you want across programming languages, methods that use default parameters should be replaced with method overloads that provide the default parameters.

(Taken from the documentation for "CA1026: Default parameters should not be used".)

Arlana answered 28/3, 2011 at 9:44 Comment(2)
Thanks, then I guess what they said in the document is wrong, right? This is what they said: "Do use member overloading rather than defining members with default arguments. Default arguments are not CLS-compliant and cannot be used from some languages. " This is the document: 1code.codeplex.com/releases/view/62267#DownloadId=215635 Just did not think the document of an semi-official project could be wrong.Lepp
Strictly speaking the default arguments are non-compliant, but it's perfectly legal to create a method with default parameters, and that method will be CLS-compliant. But that doesn't mean that other CLS-compliant languages are forced to respect the optional attributes and default values that you've specified. If you want to be sure that users of your code (in any language) can omit arguments and fall-back to the defaults that you've chosen then you should use overloading rather than default parameters.Arlana
A
8

I interpret your question to be about Optional Arguments.

If so then I believe they are CLS-Compliant and you can check by using the CLSCompliant attribute:

using System;

[assembly: CLSCompliant(true)]

namespace ConsoleApplication1
{
    public class Program
    {
        public static int Test(int val=42)
        {
            return val;
        }

        static void Main(string[] args)
        {
            System.Console.WriteLine(Test());
        }
    }
}

This compiles with no warnings.

Arevalo answered 28/3, 2011 at 9:40 Comment(2)
They're sort-of-CLS-compliant: they're permitted but a CLS-conformant language is allowed to ignore the default values and optional attribute and just treat them as normal parameters. See my answer for details.Arlana
@Arlana Thanks. Perhaps we need to able to write CLSCompliant(sort-of) ;-)Arevalo
S
0

Have a look at the CLS specs.
From page 41:

The vararg constraint can be included to indicate that all arguments past this point are optional. When it appears, the calling convention shall be one that supports variable argument lists.

But the box right below says:

CLS Rule 15: The vararg constraint is not part of the CLS, and the only calling convention supported by the CLS is the standard managed calling convention

Strongroom answered 28/3, 2011 at 9:29 Comment(2)
Note that "varargs" are a differnt beast than what the OP suggested. Varargs are essentially about the "params" keyword in C#, whereas the OPs question was about "default values for parameters" (like in void Func(int p = 42)).Emanate
@CuiPengFei: "OP" means "original poster" -- that's you. See meta.stackexchange.com/questions/15165/what-does-op-meanPliable

© 2022 - 2024 — McMap. All rights reserved.