Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 515
Cui Pengfei 崔鹏飞 Avatar asked Mar 28 '11 09:03

Cui Pengfei 崔鹏飞


People also ask

What is the feature of C?

The main features of C language include low-level access to memory, a simple set of keywords, and a clean style, these features make C language suitable for system programmings like an operating system or compiler development. Features of C Programming Language: Procedural Language. Fast and Efficient.

Is there a new function in C?

There's no new / delete expression in C. The closest equivalent are the malloc and free functions, if you ignore the constructors/destructors and type safety.

What is the newest C language?

C11. C11 is the current and latest standard of the C programming language and, as the name suggests, this standard was adopted in 2011. The formal document describing the C11 standard is called ISO/IEC 9899:2011.

What is the future of C language?

Glassdoor lists thousands of jobs in the areas of embedded systems, performance testing, firmware engineering and related fields that require C programming skills. “Programmers who understand sophisticated problem-solving techniques and can write efficient and secure code continue to be in high demand,” Grover says.


1 Answers

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

like image 173
LukeH Avatar answered Sep 18 '22 08:09

LukeH