Holy Smokes!

Friday, March 6, 2009

CLS Compliant Code

How important is it to make certain your .NET code is CLS Compliant?

For the uninformed, CLS, or Common Language Specification, refers to the claim that CLS rules and restrictions are being followed.  However, the concept has a more specific meaning depending on whether you are describing CLS-compliant code or CLS-Compliant development tools, such as a compiler.  CLS-compliant tools can help you write CLS-compliant code.

FTA Writing CLS-Compliant Code [msdn]

I have personally been taking modest steps to ensure that my code is being validated CLS compliant for some time.  I do this almost automatically.

In the real world however, how important is this really?

Technically speaking, I believe there are a lot of reasons you should be concerning yourself with this.  But realistically, is it really that important?

Say you’re one of the, I don't know, millions of developers out there working with Visual Basic.  And like most .NET developers, sooner or later you are going to consume a class library that another developer wrote.  What if the Class Library was written in another language?

Lets not forget, this is .NET, and the language used shouldn't really matter.  But let’s say the library was written in C#. and a class was defined like this.

public class TestClass : Object {
public void doSomething() {
return;
}

public void DoSomething() {
return;
}
}




Yeah, I know, this example is stupid.  But bear with me, this is an important topic.  Simplicity makes this easier to understand.



VB Developers, this is allowed in C#.  But how would VB understand this?  After all, VB is NOT case sensitive, and it would get confused.  So how could VB possibly consume this without some sort of ambiguity?



The answer is, it simply cannot.  This is where the CLS comes in.  While forcing a validation does not DO anything to your code, it will prevent you from doing ‘stupid’ things.  Lets tag up that code a little.



[CLSCompliant(true)]
public class TestClass : Object {
public void doSomething() {
return;
}

public void DoSomething() {
return;
}
}


And how does Visual Studio take this?


clscompliantfail 



Note this does not prevent the code from building, But it is telling you that the the class fails CLS compliance.  Meaning, this would probably not work against all languages.



You could however expect normal operation if it were consumed by a C# project.



This is really as hard as it gets to validate your code.



In order to use this validation in your own code, you need to be sure to mark the assembly as CLSCompliant.  This is normally done in the AssemblyInfo.cs (replace with .vb or whatever).  Note to mark an assembly, the attribute is a little different,



[assembly: System.CLSCompliant(true)]




You do not need to specify the System namespace, but the AssemblyInfo.cs does not reference the System namespace by default.  So, pick your poison, import the namespace, or specify the namespace.



So, this was just something I wanted to share, I personally find it useful, but I welcome your thoughts.



On one more side note, I would like to mention, that this validation is only really concerned with aspects of your code that can be consumed by other assemblies.  This means that if one or both of these two methods were private, it would pass validation, no problem-O.

2 comments:

Followers