Common Language Specification

In this post on Common Language Specification (CLS), you will learn another important component of .NET Architecture. In fact, it is the CLS that lets us work simultaneously with different .NET languages in a single application. In other words, Common Language Specification provides the Language Interoperability in .NET Framework.

Basically, the CLS presents a set of rules that become applicable when the assembly is generated. In other words, the CLS features are applied when the language compiler translates the source code into the MSIL code. To emphasize, the full interaction among the objects is only possible when the objects are CLS-Compliant.

In order to achieve the language interoperability, you need to write the source code in such a manner, that it remains CLS-Compliant. Further, the Framework Class Library (FCL) offers classes that are mostly CLS-Compliant. Hence, the use of FCL classes also makes your code CLS-Compliant.

The CLS Specification provides a large set of features. However, here we present some of the most commonly used features of CLS.

If you wish to find the complete details on Common Language Specification rules, refer to the Official Microsoft Documentation here.

Common Language Specification Rules

  • There are some .NET languages which don’t support unsigned data types such as VB.NET. Therefore, the use of unsigned types in a library that is to be used in a VB.NET application will result in a compile error. Hence, the unsigned integers other than Byte and the Sbyte data type are not CLS-Compliant
  • Accessibility should remain the same when overriding the inherited methods
  • When a member is itself visible and accessible, its types and members shall also be visible and accessible.
  • All elements of an Array should be CLS-Compliant a with the lower bound in each dimension should be 0.
  • Before accessing the inherited instance data, the objects should call the base class constructor.
  • Only a built-in CLS type should be the underlying type of an enum.
  • The event and its accessors should have identical visibility. Also, either keep both add and remove methods in the event definition or omit both.
  • The specific naming pattern should be used for events
  • The exception objects thrown should have a type of System.Exception or any of its inherited type.
  • CLS-Compliant interfaces should not have static methods and also they should not have fields.

Cross-language Interoperability with Common Language Specification

Now I will illustrate with examples, how the CLS provides Cross-Language interoperability. Let us suppose that, we have a class library in C# language and we want to use it in an application written in VisualBasic.NET.

Example 1

Suppose, in the C# library, a variable is declared with Unsigned Integer data type, for example, UInt32 v1; Now, this code is not compatible with VB.NET application because VB.NET doesn’t support the unsigned data types. Hence, this code is not CLS-Compliant.

Example 2

Consider the following class defined in C# language.

class MyClass
{
      public void function1(){}
      public void Function1(){}
}

The above code is not compatible with VB.NET because VB.NET is not case-sensitive. In the case of C#, both function1() and Function1() are different. However, in VB.NET they represent the same name.

Example 3

Consider the following definition of an enum type in C#. This code is also not the CLS-Compliant because it makes use of an Unsigned data type.

enum MyEnum: UInt16
    {
        A=10, B=20
    }

Now, we illustrate the concept of Common Language Specification with the following diagram. Suppose we are creating an application in VB.NET and we want to use certain libraries in our application. Further, these libraries are available in C#, F#, and C++. In that case, we can use only those classes in the library which are CLS-Compliant. To put it another way, CLS syntax is only a subset of the .NET language syntax which achieves interoperability.

Common Language Specification
Common Language Specification

Benefits of Common Language Specification

The most important benefit of the Common Language Specification that it enables interoperability in code written in different .NET languages. Apart from that, CLS offers several features that benefit programmers.

CLS not only offers the guidelines to the application programmers but also to the compiler developers so that the code can be integrated easily. Without CLS-Compliance, the library developers and programmers keep on developing the classes that can’t be used across different languages.

The use of CLS makes it possible to write the publicly accessible library of classes.

Summary

To conclude, the Common Language Specification (CLS) provides the guidelines which make language interoperability possible. It allows the programmers to write compatible code and utilize the complete functionality of the .NET Framework.


Related Topics

Getting Familiar with the Common Type System

Assemblies in .NET

Just-In-Time (JIT) Compiler

Compilation and Execution Plan of .NET

Leave a Comment

Your email address will not be published. Required fields are marked *