Click here to Skip to main content
15,885,914 members
Articles / Programming Languages / C#
Article

Non CLS-Compliant Code in C#

Rate me:
Please Sign up or sign in to vote.
4.06/5 (15 votes)
15 Jun 2011CPOL2 min read 54.2K   9   8
Non CLS-Compliant Code in C#

Introduction

Common Language Specification (CLS) is a set of basic rules and it is expected that a .NET language must satisfy them. Though C# is probably the biggest language in .NET Framework, it supports many non-CLS-compliant codes mainly to support and maintain the legacy language nativity. Writing C# programs by using only CLS-compliant code gives great portability among other .NET programs written in other .NET languages. This article tries to list non CLS-compliant codes in C#.

Using the Code

Given below are some samples for non-CLS compliant code:

  1. Two or more public / protected / protected internal members defined with only case difference:
    C#
    public int intA = 0;
    public int INTA = 2;
    C#
    public int x = 0;
    
    public void X()
    {
    }
  2. Declaring a member name preceding underscore (_) with outside visible access specifiers (public, protected, protected internal):
    C#
    public void _test()
    {
    :::::::
    }
  3. Declaring unsigned type member with access public / protected / protected internal:
    C#
    public uint a = 10;
  4. Declaring unsafe (pointer) type with access public / protected / protected internal:
    C#
    protected internal unsafe int *c;
  5. A (public / protected / protected internal) method returning a non-CLS compliant type or with parameter of non-CLS compliant type:
    C#
    public uint testmethod(int x, int y)
    {
    ::::::::
    }
    C#
    public void anothermethod(ushort x, int y)
    {
    ::::::::
    }
  6. An abstract member marked as non-CLS-Compliant in a CLS-Compliant class:
    C#
    [CLSCompliant(true)]
    public abstract class B
    {
    [CLSCompliant(false)]
    protected internal abstract void test();
    }

All the code listed above is not CLS-compliant and one should avoid this type of coding to ensure CLS-Compliant code. Many of the CLS are followed by C# and the above list mentioned some of them are not pursued by C#.

There is an attribute, called CLSCompliant, to check whether our code written is CLS-compliant or not. By giving CLSCompliant attribute with true parameter, we can get a warning message for all non CLS-Compliant code.

C#
using System;
[assembly: CLSCompliant(true)]
public class Test
{
public static int a = 10;

public int intA = 0;
public int INTA = 2;

public int x = 0;
public void X()
{
}
protected uint testmethod(int x, int y)
{
return 0;
}
}

[assembly: CLSCompliant(true)] means the CLS-Compliant is enabled to the assembly. After setting this, the compiler will check all code in the assembly for CLS-Compliant and if we want any class or method to not check for CLS-Compliant, we can give false for that member alone.

C#
using System;
[assembly: CLSCompliant(true)]
[CLSCompliant(false)]
public class clsA
{
public unsafe int* x;
}

public class clsB
{
public unsafe int* x;
}

[CLSCompliant(true)]
public class clsC
{
public unsafe int* x;
}

The clsA class will not be checked by the compiler for CLS-Compliant, but the compiler will do for clsB and clsC.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Bugrid of [CLSCompliant(true)] attribute Pin
Member 931928830-Dec-12 17:58
Member 931928830-Dec-12 17:58 
QuestionUnderscore as firsst character Pin
Kerem Kat21-Jun-11 4:45
Kerem Kat21-Jun-11 4:45 
GeneralMy vote of 5 Pin
Gary Wheeler21-Jun-11 0:22
Gary Wheeler21-Jun-11 0:22 
GeneralMy vote of 3 Pin
Ashutosh Bhawasinka20-Jun-11 20:29
Ashutosh Bhawasinka20-Jun-11 20:29 
GeneralMy vote of 1 Pin
skv_lviv20-Jun-11 16:52
skv_lviv20-Jun-11 16:52 
GeneralNice! Source? Pin
RAND 45586615-Jun-11 9:43
RAND 45586615-Jun-11 9:43 
GeneralArticle lacks depth Pin
Justin Helsley15-Jun-11 9:16
Justin Helsley15-Jun-11 9:16 
I think you should spend more time telling us why we want to write CLS compliant code and maybe some of more obscure issues that come up when trying to make your code CLS compliant. Member names / types are pretty basic examples I'm sure you can come up with more.
~Justin_H

GeneralMy vote of 1 Pin
netizenk15-Jun-11 6:07
professionalnetizenk15-Jun-11 6:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.