Search Placementyogi.com
 

Common Language Specification (CLS) in C#

The Common Language Specification (CLS) is a set of rules that describes in details the complete set of features any .NET-aware compiler must support in order to produce code that can be executed by CLR (a topic to be discussed next).

Mainly rule 1 of CTS is checked while compiling a program using .NET-aware compiler.

Rule 1: CLS rules apply only to those parts of a type that are exposed outside the defining assembly.

Simply rule 1 says that member definitions (i.e naming conventions, parameters and return types) of any language must confirm to the types available in common language specification.

Example:

public class Account
{
	Public int Calculate(int a, int b)
	{
		return a + b;
	}
}

Now to ensure the CTS-compliance of above program, only return type (int), method name (Calculate) and parameters (int a, int b) are checked. The inner logic of the program is not checked.

Few points to remember about Common Language Specification (CLS)

  • The Common Language Specification (CLS) works with the CTS to ensure language interoperability. 
  • CLS-compliant features can be applied to only public and protected members of public classes.
  • If a class exposes only CTS-compliant features then code written in any other compliant language can use this class.

How to Ensure Common Language Specification (CLS) Compliance in C#

To instruct the C# compiler to check the code for CLS-compliance, simply include the following line in your code.

[assembly: System.CLSCompliant(true)]

If there is any CLS violations in code, compiler will throw the error.

Banner to LogicGuns.com

Hide Page Information

CLS (Common Language Specification) Tutorial in C#, Learn basics of CLS in .NET, Helpful for Technical Interviews