What is the CLSCompliant
attribute?

- 15,725
- 6
- 48
- 68

- 4,511
- 7
- 27
- 17
5 Answers
You mark classes with the CLSCompliant
attribute when you want to make sure it can be used by any other .NET language.
These are the basic rules:
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
orulong
, 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.Unsafe types like pointers should not be used with
public
members. However they can be used withprivate
members.Class names and member names should not differ only based on their case. For example we cannot have two methods named
MyMethod
andMYMETHOD
.Only properties and methods may be overloaded, operators should not be overloaded.

- 4,532
- 4
- 53
- 64

- 73,752
- 17
- 161
- 228
-
Would you do that as a default for every project? – Naeem Sarfraz Feb 12 '10 at 14:43
-
4Not necessarily, mostly for those that may have exposure to other .net languages. – Otávio Décio May 11 '10 at 14:16
-
6Another restriction is _CS3006: Overloaded method 'Foo.Bar(ref Baz)' differing only in ref or out, or in array rank, is not CLS-compliant_ – Drew Noakes Mar 25 '18 at 18:40
-
3One more: public identifiers should not start with an underscore character. – Crono Nov 27 '18 at 13:26
It tells other consumers of your code that it is CLS compliant, and also makes the C# compiler check that it's CLS compliant for you.
The referenced article contains a lot more detail about what CLS compliance entails.
-
16So if I add the attribute and then build my project with no complaints, that means my project is CLS compliant and all is good? – Svish Sep 12 '12 at 14:32
-
2@Svish, yes that is the case. The compiler will let you know if you violate any rules. – Drew Noakes Mar 25 '18 at 18:34
The other answers are correct. Let me clarify some things--CLS stands for the Common Language Specification. It's the minimal set of rules and required language features that a .NET language must implement and understand. This set is a subset of the common type system, which defines how types are defined in .NET.
Being CLS compliant means that you can write code that can be consumed by any language that can be compiled and run on the CLR. But CLS compliance is not required, giving you the flexibility in cases where CLS compliance would be hard or impossible to do.
If you intend your code to be consumed by other developers, your API (your public classes and methods) should be CLS compliant. You should declare this by adding the CLSCompliantAttribute to your assemblies. If you are not writing for others, CLS compliance is not necessary, although FxCop (Framework Cop) would disagree with me.
When your assembly is marked with the CLSCompliantAttribute, the compiler will (should!) check your code to see if, when compiled, it will violate any of the CLS rules (some of which ocdecio mentioned) and report violations to you for fixing.

- 30,738
- 21
- 105
- 131
-
1Unfortunately the grand-vision of the CLR having many different frontend languages (like VB.NET - which dying a slow death - and others now gone, like MC++, Delphi.NET, Oxygene, J#, JScript.NET, and so on) limits the benefits of completing CLSCompliance given that the other remaining languages that support the CLR have no problem supporting non-CLSCompliant features like optional parameters and `out` parameters - so I think Microsoft needs to revisit the CLS's requirements and perhaps loosen them. – Dai Oct 12 '19 at 06:59
-
CLS compliant is a subset of the full language spectrum allowed by the CLR. It restricts it to subsets that are likely available by the majority of languages that target the CLR. This increases, but does not guarantee, that your library can be used by all languages targeting the CLR.

- 733,204
- 149
- 1,241
- 1,454
As it fits here: To mark a whole project CLS compliant add this line to AssemblyInfo.cs
(can be found under Properties in Solution Explorer)
[assembly:CLSCompliant(true)]
or equivalently in VB.NET (AssemblyInfo.vb
is hidden under My Project)
<Assembly: CLSCompliant(True)>
Thanks to Making Your Code CLS Compliant.

- 672
- 7
- 15