Argument type is not CLS-compliant, why?

This is an old question, but I thought a better explanation was due for future investigators (such as myself).

First off, the links in the other answers provide great detail into the reason this warning is given.

However, to summarize, code written for the Common Language Runtime (such as C#) is CLS-Compliant if it can interface with other languages designed for the CLR. This means that certain datatypes specific to the language that are not common to the entire runtime are not compliant. The quick and easy fix for this in regards to variables and methods is to give them the visibility modifier internal which specifies that the method, class, property, etc. is not visible outside of the assembly it is being built for. This should only be done on those items which you do not need or want to be used outside of the assembly; for those that you want visible, use datatypes that are CLS-compliant.


In your AssemblyInfo.cs file, you've probably got a line that reads

[assembly:CLSCompliant(true)]

If you do, then the following rules must be met. (Copy-Pasta from http://www.devarticles.com/c/a/C-Sharp/Making-Your-Code-CLS-Compliant/)

  1. Unsigned types should not be part of the public interface of the class. What this means is public fields should not have unsigned types like uint or ulong, public methods should not return unsigned types, parameters passed to public function should not have unsigned types. However unsigned types can be part of private members.

  2. Unsafe types like pointers should not be used with public members. However they can be used with private members.

  3. Class names and member names should not differ only based on their case. For example we cannot have two methods named MyMethod and MYMETHOD.

  4. Only properties and methods may be overloaded, Operators should not be overloaded.


Some more info:

  • Writing CLS-Compliant Code

And you can get those warnings in a CLS-compliant assembly, that references a non-CLS-compliant assembly (and exposes its non-CLS-compliant members):

  • Can an assembly that includes a non-CLS-compliant reference be CLS-compliant?

Tags:

C#