What is the 'CLSCompliant' attribute in .NET?

.NetCls Compliant

.Net Problem Overview


What is the CLSCompliant attribute?

.Net Solutions


Solution 1 - .Net

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:

  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.

Solution 2 - .Net

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.

Solution 3 - .Net

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.

Solution 4 - .Net

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.

Solution 5 - .Net

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPraveen SharmaView Question on Stackoverflow
Solution 1 - .NetOtávio DécioView Answer on Stackoverflow
Solution 2 - .NetJon SkeetView Answer on Stackoverflow
Solution 3 - .Netuser1228View Answer on Stackoverflow
Solution 4 - .NetJaredParView Answer on Stackoverflow
Solution 5 - .NetMarkus HartmairView Answer on Stackoverflow