Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CLS allows private portions of the code in classes to be non CLS compliant. How is this possible?

Tags:

c#

.net

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 ?

like image 419
teenup Avatar asked May 29 '10 10:05

teenup


2 Answers

CLS stands for Common Language Specification. To simplify, it's basically the minimal types and identifiers that all .NET languages must know about. 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-compliance 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.

like image 63
Julien Lebosquain Avatar answered Oct 17 '22 04:10

Julien Lebosquain


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.

like image 28
this. __curious_geek Avatar answered Oct 17 '22 06:10

this. __curious_geek