It is the C # code in line with the. NET Command Language Specification (CLS)? In this document, Sriram shows us how to check and correct the code for compliance.Introduction If you are writing. Net classes, which will be used by others. Net regardless of the language lessons are applied, then the code should conform to the CLS [Common Language Specification]. This means that the class should expose only the features that are common to all. Net Languages. The following are the basic rules to follow when writing a complaint CLS C # code.
- The unsigned types should not be part of the public interface of the class. What this means public fields should not have unsigned types as a uint or ulong, public methods should not return unsigned types, parameters passed to the civil service should not sign types. However, unsigned types can be part of the private members.
- Unsafe types like pointers should not be used with members of the public. However, they can be used for private members.
- The class names and member names should not differ only in their case. For example, we can not have two methods called MyMethod and MyMethod.
- Only properties and methods can be overloaded, operators should not be overloaded.
Those rules must be followed only for the types and members that are exposed publicly for his program. Private lessons, private methods and local variables have to follow the defect rules.By the C # compiler does not verify compliance with the code CLS. Must explicitly checking the C # compiler for the performance of CLS using CLSCompliantAttribute class. By specifying the truth value of this attribute specifies that the C # compiler must verify compliance with the code CLS. The CLSCompliantAttribute can be applied to assemblies, modules, types, and members.
Example:
- using System;
- //setting CLSCompliant attribute to true
- [assembly:CLSCompliant(true)]
- [CLSCompliant(true)]
- public class Test
- {
- public void MyMethod()
- {
- }
- //error because methods differ only in their case
- public void MYMETHOD()
- {
- }
- static void Main()
- {
- }
- }
Compiling the above code will result in the following error:
Error CS3005: Identifier Test.MYMETHOD '( )' differing only in case is not CLS-compliant
A section of the program can not be marked as CLSCompliant if the section, which contains, not marked as CLSCompliant. For example, a class can not be marked as CLSCompliant if the Assembly is not marked for CLSCompliant.
0 comments:
Post a Comment