What is the CLSCompliant
attribute?
The CLSCompliantAttribute attribute is used to indicate whether a particular program element complies with the Common Language Specification (CLS), which defines the features that any language that targets . NET must support.
If you are writing . Net classes, which will be used by other . Net classes irrespective of the language they are implemented, then your code should conform to the CLS [Common Language Specification]. This means that your class should only expose features that are common across all .
You mark classes with the CLSCompliant
attribute when you want to make sure it can be used by any other .NET language.
These are the basic rules:
Unsigned types should not be part of the public interface of the class. What this means is public fields should not have unsigned types like uint
or ulong
, public methods should not return unsigned types, parameters passed to public function should not have unsigned types. However unsigned types can be part of private members.
Unsafe types like pointers should not be used with public
members. However they can be used with private
members.
Class names and member names should not differ only based on their case. For example we cannot have two methods named MyMethod
and MYMETHOD
.
Only properties and methods may be overloaded, operators should not be overloaded.
It tells other consumers of your code that it is CLS compliant, and also makes the C# compiler check that it's CLS compliant for you.
The referenced article contains a lot more detail about what CLS compliance entails.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With