Why should I write CLS compliant code?

C#Cls Compliant

C# Problem Overview


I've found a lot of pages about CLS compliance.

I've understood that CLS compliance:

Many peolple write that "if you write code, you should write it CLS compliant." But as far I can read, there is no reason to use CLS compliance in generic software.

Am I right, or did I miss something?

C# Solutions


Solution 1 - C#

If you write a library or framework it makes sense to ensure your library can be used from any CLR language.

Solution 2 - C#

CLS-compliance is particularly important if you're distributing libraries - specifically, writing CLS compliant guarantees that your libraries will be usable by all CLS-compliant languages.

For instance, Visual Basic is not case-sensitive, whereas C# is. One of the requirements of CLS compliance is that public (and protected) member names should not differ only by case, thus ensuring that your libraries can be used safely by Visual Basic code, or any other .NET language that doesn't differentiate based on case.

Solution 3 - C#

The answer is to allow maximum compatibility across .NET languages. CLS is the lingua franca that allows C# assemblies to work with F#, Iron Python, C++/CLI, VB.NET, Boo and all the other .NET languages. Step outside that boundary and your assembly may work correctly, but not necessarily.

Solution 4 - C#

There may not be a specific reason to have your code be CLS compliant, but people are referring to it being a "best practice"--something that you should do because it's a good habit, rather than being measurably better for a particular scenario.

In other words, it's a good idea to make your code CLS compliant unless you have a reason not to.

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
QuestionLucaView Question on Stackoverflow
Solution 1 - C#Remus RusanuView Answer on Stackoverflow
Solution 2 - C#DathanView Answer on Stackoverflow
Solution 3 - C#plinthView Answer on Stackoverflow
Solution 4 - C#Adam RobinsonView Answer on Stackoverflow