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 .Net languages. The following are the basic rules that should be followed when writing a CLS complaint C# code.
1. 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.
2. Unsafe types like pointers should not be used with public members.
However they can be used with private members.
3. Class names and member names should not differ only based on their case.
For example we cannot have two methods named MyMethod and MYMETHOD.
4. Only properties and methods may be overloaded, Operators should not be overloaded.
The above-mentioned rules should be followed only for the types and member that are
publicly exposed by your program. Private classes, private methods and local variables need to follow the rules.
By default the C# complier does not check for CLS compliance of the code.
We should explicitly make the C# compiler check for CLS compliance by using the CLSCompliantAttribute class. By specifying the value of true to this attribute we specify that the C# compiler should check for the CLS compliance of the code. The CLSCompliantAttribute can be applied to assemblies, modules, types, and members.
To make an assembly CLSCompliant, add the following line in the AssemblyInfo.cs file of that project -
[assembly: CLSCompliant(true)]
No comments:
Post a Comment