Common Language Specification in .NET Framework

Common Language Specification in .NET Framework

In this article, I am going to discuss Common Language Specification (CLS) in .NET Framework. Please read our previous article, where we discussed the Common Type System (CTS) in .NET Framework with example.

What is Common Language Specification in .NET Framework?

CLS is nothing but it is a set of guidelines or rules. When these set of rules or guidelines are followed by any dot net supported programming language, then that code is guaranteed to be consumed by other dot net programming language. For example, if you write C# code and if you ensure that you are following the Common Language Specifications, then that C# code can be consumed by any other dot net supported programming language.

In order to understand this concept, what we will do here is, we will violate the common language specifications and then we will see what happens.

Example to understand CLS:

As we know C# is case sensitive whereas VB is not case sensitive. That means in C#, you can declare the same member name multiple times with case differences but it is not possible in VB. So, let us create two class library projects. One using C# and other using VB. Then we will try to consume the C# class library project in the VB class library project.

Creating a C# Class Library Project:

Create a class library project with the name CsharpClassLibrary using the C# programming language. Once you create the C# class library project then add a class called Calculator and then copy and paste the following code in it. As you can see we have created two methods with the same name but with case differences. One method starts with capital C while the other one starts with a small c.

namespace CsharpClassLibrary
{
    public class Calculator
    {
        public int Calculate()
        {
            int a = 10, b = 20;
            int c = a + b;
            return c;
        }

        public int calculate()
        {
            int a = 10, b = 20;
            int c = a + b;
            return c;
        }
    }
}
Creating VB Class Library Project:

To the same CsharpClassLibrary solution, let us add another class library project with the name as VBClassLibrary but using VB as the programming language. Here, in this project we want to use the C# class library project, so first add a reference to the CsharpClassLibrary project. Then create a class with the name TestClass and copy-paste the following code in it.

Imports CsharpClassLibrary

Public Class TestClass
    Public Sub TestMethod()
        Dim obj As New Calculator()
        obj.Calculate()
    End Sub
End Class

Now, when you try to build the VB Class Library project, you will get the below error. This is because VB is not case sensitive and it found two methods with the same name. That means we are violating the Common Language Specifications in C# code.

‘Calculate’ is ambiguous because multiple kinds of members with this name exist in class ‘Calculator’.

Now, let us change the second method name as Calculate2 as shown below.

namespace CsharpClassLibrary
{
    public class Calculator
    {
        public int Calculate()
        {
            int a = 10, b = 20;
            int c = a + b;
            return c;
        }

        public int Calculate2()
        {
            int a = 10, b = 20;
            int c = a + b;
            return c;
        }
    }
}

With the above changes in place, now, build the VB class library project and the build should succeed as expected. Now, you may have one question, how to check whether the code is CLSCompliant or not.

How to check the code is CLS Compliant or not?

In order to check whether your code is following the Common Language Specifications or not, first, you have to enable CLS Compliant in AssemblyInfo.cs file. So, go to the C# Class Library Project and open the AssemblyInfo.cs file which is present inside the Properties folder.

Once you open the AssemblyInfo.cs class file, then follow the below 2 steps.

Step1: Import the System namespace as
using System;

Step2: Add the following CLSCompliant attribute at the bottom of this file and set its value to true
[assembly: CLSCompliant(true)]

With the above changes in place in the AssemblyInfo.cs file, now modify the Calculator class as shown below.

namespace CsharpClassLibrary
{
    public class Calculator
    {
        public int Calculate()
        {
            int a = 10, b = 20;
            int c = a + b;
            return c;
        }

        public int calculate()
        {
            int a = 10, b = 20;
            int c = a + b;
            return c;
        }
    }
}

Now, when you build the C# Class Library Project you will get the following warning.

Common Language Specification in .NET Framework

In the next article, I am going to discuss the Managed and Unmanaged Code in .NET Application in detail. Here, in this article, I try to explain Common Language Specification in .NET Framework in detail and I hope you enjoy this article.

Leave a Reply

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