Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the 'CLSCompliant' attribute in .NET?

What is the CLSCompliant attribute?

like image 292
Praveen Sharma Avatar asked Feb 20 '09 17:02

Praveen Sharma


People also ask

What is 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.

What is CLS compliant in C#?

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 .


2 Answers

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:

  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.

like image 140
Otávio Décio Avatar answered Oct 17 '22 13:10

Otávio Décio


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.

like image 34
Jon Skeet Avatar answered Oct 17 '22 14:10

Jon Skeet