Home arrow C# arrow Making Your Code CLS Compliant
C#

Making Your Code CLS Compliant


Is your C# code in line with the .NET Command Language Specification (CLS)? In this article, Sriram shows us how to check and fix our code for compliance.

Author Info:
By: Sriram Vaideeswaran
Rating: 5 stars5 stars5 stars5 stars5 stars / 89
August 12, 2003

print this article
SEARCH DEVARTICLES

Is your C# code in line with the .NET Command Language Specification (CLS)? In this article, Sriram shows us how to check and fix our code for compliance.Introduction

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 .Net languages. The following are the 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() 


If you mark an assembly as CLSCompliant and if any of the publicly exposed types of members are not CLS compliant then the compiler would raise an error as shown in the following example.

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 program section cannot be marked as CLScompliant if the section, which encloses it, is not marked as CLScompliant. For example, a class cannot be marked as CLSCompliant if the assembly is not marked for CLScompliant.

Example

using System;
//setting CLSCompliant attribute to 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:

'Test' cannot be marked as CLS compliant because the assembly is not marked as compliant

You can also mark particular section of a CLS compliant class as non-CLS compliant by setting the CLSCompliantAttribute to false for the particular section. The compiler will not enforce CLS compliance for sections, which are marked as non-CLS compliant.

Example

using System;
//setting CLSCompliant attribute to true
[assembly:CLSCompliant(true)]
[CLSCompliant(true)]
public class Test
{
    public void MyMethod()
    { 
    }
    //Not an error because the methods is not CLS complaint
[CLSCompliant(false)]
    public void MYMETHOD()
    {
    }
    static void Main()
    {
    }


Compiling the above code will not result in an error, as the method is marked as non-CLS compliant.

Following are the some situations which may cause the C# compiler to raise an error 

1. If your class methods that are identical but differ only by ‘out’ or ‘ref’.

2. If a publicly exposed member starts with an underscore (_).

3. If an abstract member is marked for non-CLS compliance within a CLS compliant class.

Conclusion

If you develop applications that conform to the CLS, your program will be interoperable with a wide range of .Net supported programming languages. Therefore, your application is likely to have a wider customer base than a non-CLS-compliant version of your application. You can make sure that the C# programs you develop are CLS compliant by using the CLSCompliantAttribute, which will cause the compiler to raise errors if the program is not CLS compliant.

DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

All C# Tutorials
More By Sriram Vaideeswaran


blog comments powered by Disqus
C# ARTICLES

- Introduction to Objects and Classes in C#, P...
- Visual C#.NET, Part 1: Introduction to Progr...
- C# - An Introduction
- Hotmail Exposed: Access Hotmail using C#
- Razor Sharp C#
- Introduction to Objects and Classes in C#
- Making Your Code CLS Compliant
- Programming with MySQL and .NET Technologies
- Socket Programming in C# - Part II
- Socket Programming in C# - Part I
- Creational Patterns in C#
- Type Conversions
- Creating Custom Delegates and Events in C#
- Inheritance and Polymorphism
- Understanding Properties in C#

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials