The .NET CLS (Common Language Specification) allows a programmer to write code in one .NET language and confidently know that all other .NET languages can safely interact with and even utilize data and methods from the original project.
In Troelsen and Japikse's, C# 6.0 and the .NET 4.6 Framework, the authors write that you cannot have CLS non-compliant parameters or return values for a method but you can have CLS non-compliant variables inside a method yet the method still be CLS compliant.
For example (pg. 19),
class Calc
{
// Exposed unsigned data is not CLS compliant!
public ulong Add(ulong x, ulong y)
{
return x + y;
}
}
is not CLS compliant because of the ulong
parameters. But (pg. 20),
If you were to only make use of unsigned data internally in a method, as follows:
class Calc
{
public int Add(int x, int y)
{
// As this ulong variable is only used internally,
// we are still CLS compliant.
ulong temp = 0;
...
return x + y;
}
}
you have still conformed to the rules of the CLS and can rest assured that all .NET languages are able to invoke the Add() method.
What I don't understand is that what if the CLS non-compliant variable interacts with one of the parameters that is returned in the result of the method? Wouldn't this break the non-compliance?
Thanks.
My intuition is that for this example, the only thing that would "break" the compliance of the CLS type int
would be if some math operator caused overflow w/ the int
variable- which would then be an issue during compilation or runtime, not with conformity of the CLS.