78

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?

Community
  • 1
  • 1
Luca
  • 11,646
  • 11
  • 70
  • 125

4 Answers4

59

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

Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569
  • CIL (Common Intermediate Language) is not sufficient? – Luca Dec 01 '09 at 20:17
  • 6
    afaik no, because CIL may contain features that are unusable from certain CLR languages. Eg. you have a method that takes UInt32 parameter, the CIL code is fine but the method cannot be invoked from a language that has no concept of unsigned. – Remus Rusanu Dec 01 '09 at 20:25
  • What happens is the language is "translated" in CIL (having the source code, of course)? In this scenario, it seems CLS compliance is required only with legacy assemblies, right? – Luca Dec 01 '09 at 20:29
  • No, it is required if the DLL is to be used from any langauge that only supports the CLS SUBSET of the CLR functionality. – TomTom Mar 09 '10 at 20:33
  • 2
    @TonyD [Common Language Specification](https://msdn.microsoft.com/en-us/library/vstudio/12a7a7h3%28v=vs.100%29.aspx) – Remus Rusanu May 22 '15 at 16:59
  • 3
    Not to nitpick, @TonyD, but its probably easier to just google that than ask it in a comment on here. https://msdn.microsoft.com/en-us/library/system.clscompliantattribute(v=vs.110).aspx – Dan Csharpster Sep 11 '15 at 15:27
50

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dathan
  • 7,266
  • 3
  • 27
  • 46
  • 2
    There's also a rule that you can't start members with underscores. Which leaves me wondering how to name a protected variable that is wrapped by a public property... Can't use a lowercase version, can't use an underscore... what's the next ugly alternative? – Lilith River Oct 12 '11 at 10:44
  • Probably prefixing it with a p. Not pretty, but functional. While I prefer C# Properties over Java-style getters and setters, at least in Java the naming convention is easier. – Dathan Oct 14 '11 at 11:55
  • Somehow, when it's all said and done, java ends up being more concise. I think it's probably more the libraries than the language, though. – Lilith River Oct 15 '11 at 22:38
  • PowerShell is also case-insensitive, which has always left me wondering how it'd behave when a .NET class contains members differ only in case – phuclv Apr 19 '20 at 16:00
22

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.

plinth
  • 48,267
  • 11
  • 78
  • 120
11

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.

Adam Robinson
  • 182,639
  • 35
  • 285
  • 343
  • I find it very strange that many .NET libraries aren't CLS compliant. So pretty much any class that uses one of those components isn't compliant either. – MrFox Nov 18 '20 at 21:19
  • @MrFox: Just *using* such a component is fine. *Publicly exposing* it makes your class non-compliant. – Heinzi Apr 13 '23 at 13:01