Disable CLS compliance checking in C#

user429400 picture user429400 · Nov 1, 2010 · Viewed 7.6k times · Source

I'm working on code that have the following attributes on some of its methods:

[CLSCompliantAttribute(false)] 

How is it that when I build the code as is, I see that the compliance checking is being performed, and when I comment it out, it seems that the compliance checking is NOT being performed?

I've expected the opposite behavior...

Answer

SLaks picture SLaks · Nov 1, 2010

Adding [CLSCompliant(false)] marks the member you add it to as non-compliant.

If you mark the member as non-compliant, the compiler will not warn you if it isn't compliant. (Since you already said that it's not compliant.)

If, however, the member is marked as compliant (either explicitly or indirectly from an assembly-level attribute), but it is in fact not compliant (for example, it takes a uint), the compiler will warn you (since the attribute is now lying about the member).