CLS Rules
Writing code in accordance with CLS
Update: November 2007
In general, support for Common Language Specification (CLS) refers to the requirement that CLS rules and restrictions are followed. However, the concept has a more specific meaning, depending on whether you are describing CLS compliant code or programming tools compatible with CLS, as a compiler. CLS compatible tools can help write code compatible with CLS.
Code CLS
Public class definitions.
Definitions of the public members of public classes and members accessible by derived classes (family access).
Parameters and return types of public methods of public classes and methods accessible to derived classes.
Need not comply with the rules CLS features that used in the definitions of private classes, definitions of private methods for public classes and local variables. You can also use the language features you want in the code that implements the class and still have a CLS compatible component.
bhc3fa7f.alert_note (en-us, VS.90) .gifNota:
The staggered matrices, ie arrays of arrays are compatible with CLS. In version 1.0 of the .NET Framework, the C # compiler erroneously reports that are not.
You can mark assemblies, modules, types and members as compatible or not compatible with CLS by CLSCompliantAttribute. All assembled desired compatible with CLS must be marked as such. An assembly that is not marked as CLS is considered to be not CLS. If no CLS attribute to a type is applied, it is assumed that such is the same CLS compliance as the assembly where the type is defined. Similarly, if no CLS attribute is applied to a member, it is considered that the member has the same compatibility with CLS that the type that defines it. You can not mark a program element as CLS if the enclosing element is not marked as CLS. In the example at the end of this topic using CLSCompliantAttribute illustrated.
Assemblies, modules and types can be CLS-compliant even if some parts of the assembly, module, or are not compatible with CLS, as long as two conditions are met:
If the item is marked as CLS, the parts that are not compatible with CLS should be marked by CLSCompliantAttribute, establishing his argument false.
For each member that is not compatible with CLS, you must supply a comparable reciprocating member that is compatible with CLS.
If you design a library compatible with CLS class, ensures that the library will be interoperable with a variety of programming languages; therefore, probably, the library will have a wider than a version that is not compatible with CLS customer base.
.NET Framework provides a library of classes with CLS compliant. For more information about designing classes, see Reference Class Library .NET Framework.
Tools compatible with CLS
Tools compatible with CLS consumer.
Consumer tools are languages that allow programmers to access all the features supplied by CLS-compliant libraries. It is possible that programmers using these languages can not extend CLS compliant libraries by creating new types, but can use any type defined by a compatible library. The level of support can be useful when you want to access a class library .NET Framework but no need to create new objects for consumption by others, as when Web Forms are used in an ASP.NET page or when a user interface Windows Form is created.
Tools extender compatible with CLS.
The extensor tools are languages that allow programmers to use and extend types defined in CLS compliant libraries. Developers can use existing types, and define new types. The extensor tools must comply with all rules following consumer tools as well as some additional rules described in the specification of Common Language Infrastructure, Partition I - Architecture, available on the website Microsoft Developer Network (.
When compatible with CLS own components are designed, can be useful to use a tool compatible with CLS. Writing CLS-compliant components without this aid is more difficult because you may not have access to all CLS features you want to use.
Some compilers CLS compliant languages such as C # compiler or Visual Basic, allow you to specify that the code should be compatible with CLS. These compilers can check the compatibility with CLS and report when the code uses functionality not supported by CLS. Compilers for C # and Visual Basic allow you to mark a program element as CLS, which causes the compiler to generate an error at compile time if the code is not CLS. For example, the following code generates a compiler warning:
C # VB
using System;
[assembly: CLSCompliant (true)]
[CLSCompliant (true)]
{public class MyCompliantClass
// ChangeValue Exposes UInt32, Which is not in CLS.
// A compile-time warning results.
public void ChangeValue (UInt32 value) {}
int i = 2;
Console.WriteLine (i);
}
}
warning CS3001: Argument type 'uint' is not CLS-compliant
or the following warning in Visual Basic:
warning BC40028: Type of parameter 'value' is not CLS-compliant.
To remove the warning, it may indicate that ChangeValue is not supported, as shown in the following example.
C # VB
using System;
[assembly: CLSCompliantAttribute (true)]
[CLSCompliantAttribute (true)]
{public class MyCompliantClass
// Method marked as not compliant.
[CLSCompliantAttribute (false)]
public void ChangeValue (UInt32 value) {}
int i = 2;
Console.WriteLine (i);
}
}