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?
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".)
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.
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