Writing CLS-Compliant Code in C#

The syntax of C# ensures that many CLS rules can’t be broken. For example, C# doesn’t permit the declaration of global static fields or non-zero-based arrays, both requirements of CLS compliance. However, some CLS rules can’t be enforced by the language, and guaranteeing that they’re followed is the responsibility of the programmer. The following list highlights some key requirements of writing CLS-compliant code in C#:

  • Case must not be used to differentiate between program element identifiers such as member names.

  • All member parameter and return types must be CLS-compliant; this means the following simple types cannot be used: sbyte, ushort, uint, and ulong.

  • Pointer types and function pointers aren’t CLS-compliant.

  • The underlying type of an array must be CLS-compliant.

  • The underlying type of an enumeration must be one of byte, short, int, or long.

  • A comparable CLS-compliant element should be provided for a noncompliant element.

  • CLS-compliant program elements and their non-CLS-compliant members must be identified using the CLSCompliant attribute (discussed in the next section).

Note

The CLS specifies that variable parameter lists cannot be used. This doesn’t apply to C# parameter arrays (identified with the param keyword) because the C# compiler maps these to fixed-size arrays.

More Info

For a complete list of CLS rules, consult the Common Language Infrastructure, which is available as standard ECMA-335 from the ECMA Web site, http://www.ecma.ch.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset