What is CLS (Common Language Specification) in .NET?
π‘ Concept Name
CLS (Common Language Specification)
π Quick Intro
CLS defines a set of rules and guidelines that .NET languages must follow to ensure interoperability. Code that follows CLS can be used across all .NET languages like C#, F#, and VB.NET.
π§ Analogy / Short Story
Think of CLS as a "universal handshake" between .NET languages. If everyone follows the same handshake rules, they can all understand and work with each otherβregardless of their native language (C#, F#, etc.).
π§ Technical Explanation
- π CLS is a subset of the Common Type System (CTS).
- π It defines the minimal features needed for cross-language compatibility.
- π« Certain C# features like unsigned types (`uint`, `ulong`) are not CLS-compliant.
- β CLS-compliant code is more reusable and accessible from other .NET languages.
- π§ͺ Use `[CLSCompliant(true)]` attribute to enforce compliance during compilation.
π― Purpose & Use Case
- β Enable code sharing across different .NET languages.
- β Ensure public APIs can be consumed universally.
- β Promote standardization and interoperability within large teams/projects.
π» Real Code Example
Enforce CLS compliance and avoid non-compliant types like `uint`:
[assembly: CLSCompliant(true)]
public class MathLib
{
// β Not CLS-compliant (uint)
// public uint Add(uint a, uint b) => a + b;
// β
CLS-compliant
public int Add(int a, int b) => a + b;
}

β Interview Q&A
Q1: What is CLS in .NET?
A: A specification that defines rules for language interoperability.
Q2: Why is CLS needed?
A: To ensure code written in one .NET language can be used in another.
Q3: Is `uint` CLS-compliant?
A: No.
Q4: How do you enable CLS checks in code?
A: Use `[assembly: CLSCompliant(true)]` attribute.
Q5: Is CLS a runtime or compile-time feature?
A: Compile-time.
π MCQs
Q1. What does CLS stand for in .NET?
- Common Library System
- Code Linking Structure
- Common Language Specification
- Core Language Service
Q2. What is the goal of CLS?
- Code optimization
- Low memory usage
- Language interoperability
- Runtime validation
Q3. Which type is NOT CLS-compliant?
- int
- string
- uint
- bool
Q4. What attribute enables CLS compliance checking?
- [CLSCheck]
- [assembly: CLSCompliant(true)]
- [VerifyCLS]
- [Compliant]
Q5. CLS is a subset of?
- JIT Compiler
- Common Language Runtime
- Common Type System
- Intermediate Language
π‘ Bonus Insight
When building public libraries or SDKs meant for wide reuse, making your code CLS-compliant ensures that all .NET languages can consume it. It's a best practice even if you're only writing in one language initially.
π PDF Download
Need a handy summary for your notes? Download this topic as a PDF!