Wednesday, November 10, 2010

what is cls compliant code

cls compliant is mostly used when we wants our dll or any other code to be used by other language which are supported by .net.
lets say we wants to create a library in c# which we wants to be use in c# , vb.net , visual c++ or any other such languages. so for that we will have to create it as a cls compliant so that it can be consumed by all other .net supported language.

You can apply the CLSCompliant attribute on a class, an assembly (or a program element) and have the compiler check if your code is CLS (Common Language System) compliant. This means it works properly when consumed by other .NET languages. For example, you can place the following attribute on your .NET AssemblyInfo.cs files:

[assembly: CLSCompliant(true)]

Some of the things the compiler checks:

Class and member names cannot differ only by case. For example, you can't have one property named Counter and another named counter. This is important for cross-language compatibility since VB .NET isn't case sensitive.

Overloaded class methods cannot differ only by out or ref parameter designations.

Publicly exposed members cannot start with an underscore ( _ ).

Operators can't be overloaded

Unsigned types can't be part of the public interface of a class

Unfortunately, although you can apply the CLSCompliant attribute in VB .NET, the VB .NET compiler doesn't check for CLS compliance. In VB.NET 2005, this has apparently been fixed.


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.

For marking an entire assembly as CLS compliant the following syntax is used

using System;
[assembly:CLSCompliant(true)]

For marking a particular method as CLS compliant the following syntax is used

[CLSCompliant(true)]
public void MyMethod()


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()
{
}
}

Share This!


No comments:

Powered By Blogger · Designed By Seo Blogger Templates