CLS allows private portions of the code in classes to be non CLS compliant. How is this possible, because ultimately the code needs to be converted to the IL ?
views:
76answers:
4CLS allows private portions of the code in classes to be non CLS compliant. How is this possible ?
CLS stands for Common Language Specification. To simplify, it's basically the minimal types and identifiers that all .NET languages must know. For example, Int32 is CLS-compliant: any .NET language must be able to handle it. UInt32 is not. Since your code is private, it won't be accessed by other assemblies in other languages so it doesn't matter if it is CLS-compliant or not.
See this MSDN page for more information about CLS-compliance.
Edit: I think you misunderstand what CLS compliance is. It's not about whether the code can be compiled into IL or not. UInt32 can be used in IL. So does an identifier named '©'. CLS-compliant is just a minimal contract for language interoperability. What the CLR supports is way broader than the limitations for CLS compliance.
Edit2: Yes, you're right. A .NET language is required to support Int32, not UInt32, even if has a direct mapping to IL. See pointers, they're supported in IL and in C# but are not CLS compliant. VB.NET doesn't implement a support for them and still is CLS-compliant.
No - The CLR does not need to know what's happening under the hood. You need to have only your public classes and public/private members of those public classes to be CLS compliant, rest code will never be accessed - it will remain inaccessible to other languages that use your classes.
The CLS's only real goal is to ensure that any compliant language is able to interact with any compliant class, regardless of the language it was written in, cause that class's API by definition requires only things that all languages must support. Hence, "Common Language Specification".
Semi obviously, the private API is (or at least, should be) invisible outside your own code, so it doesn't matter whether it's CLS compliant or not.
If it's CLS compliant that means every .NET language must support it.
If it is representable in IL that means that some .NET languages may support it.
One represents a requirement of .NET languages and the other represents what is possible in .NET languages.