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.
1 Answer 1
The CLS compliant function could have been written as
class Calc
{
public int Add(int x, int y)
{
ulong temp_x = x;
ulong temp_y = y;
// Perform calculation using unsigned math
ulong temp_result = temp_x + temp_y;
// Convert back to signed int for CLS compliance
return (int)temp_result;
}
}
The reason that a CLS compliant function can not use ulong
is because not all .NET languages have unsigned types and those languages would be unable to call Add(ulong, ulong)
or interpret an unsigned return value.
The use of types that are not universally supported by .NET languages inside your functions is completely free and depends only on the capabilities of the language you are writing the code in. Other languages don't see the code inside the functions, so it doesn't matter if that code uses features that are not universally supported.
-
11) Your code does not compile, "CS0266 Cannot implicitly convert type 'int' to 'ulong'. An explicit conversion exists (are you missing a cast?)". 2) After fixing your code by casting
x
andy
toulong
, it returns a value forAdd(int.MaxValue, int.MaxValue)
which normally would give a compiler error, is this still CLS compliant?Quantic– Quantic01/27/2017 19:16:18Commented Jan 27, 2017 at 19:16 -
1@Quantic: Re 2: It is still CLS compliant, because the CLS can not require compile errors without effectively restricting the .NET framework to a single language.Bart van Ingen Schenau– Bart van Ingen Schenau01/27/2017 20:38:14Commented Jan 27, 2017 at 20:38